DEV Community


How to Create SECRET_KEY for Django Settings

serhatteker profile image Serhat Teker Originally published at on ・1 min read

When you start a django project, django-admin startproject automatically adds a randomly-generated SECRET_KEY to each new project.

However if you want to change it, or add a seperate one to each of your
environment, e.g: one for 'production', one for 'staging', one for 'production' etc, how do you gerenerate a new ones?

There would be another case: you cloned a project from a remote repo and want to change the default SECRET_KEY.

For sure there are some other solutions but I believe that the best solution comes from native Django itself; get_random_secret_key:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from import utils


When we run above codes it returns a 50 character random string usable as a SECRET_KEY setting value.

In the above snippet as you can see we are using get_random_secret_key function from django's utils module.

In addition to that if you'd like to use it as a 'one-liner' command run it like below:

$ python shell -c 'from import utils; print(utils.get_random_secret_key())'

All done!

Discussion (0)

Editor guide