Create Django Secret Environment Variable¶
In settings.py, the Django secret key configuration is in an environment variable. See below.
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ("DJANGO_SECRET_KEY")
Create a Django Secret key.
python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'
You will see an output similat to this.
w#5cb$au3$t+hyj(f7ejgrk7$xet7_q@8m)5qd*c_*)nl1shwr
Danger
DO NOT USE THE NO LONGER A SECRET, SECRET KEY GENERATED HERE.
Copy and add the secret key to an environment variable.
export DJANGO_SECRET_KEY='w=#5cb$au3$t+hyj(f7ejgrk7$xet7_q@8m)5qd*c_*)nl1shwr'
printenv DJANGO_SECRET_KEY # To check it worked
w#5cb$au3$t+hyj(f7ejgrk7$xet7_q@8m)5qd*c_*)nl1shwr
export DJANGO_SECRET_KEY='w=#5cb$au3$t+hyj(f7ejgrk7$xet7_q@8m)5qd*c_*)nl1shwr'
printenv DJANGO_SECRET_KEY # To check it worked
w#5cb$au3$t+hyj(f7ejgrk7$xet7_q@8m)5qd*c_*)nl1shwr
Right-click the Computer icon and choose Properties, or in Windows Control Panel, choose System.
Choose Advanced system settings.
On the Advanced tab, click Environment Variables.
Click New to create a new environment variable.
After creating or modifying the environment variable, click Apply and then OK to have the change take effect.
Note
The graphical user interface for creating environment variables may vary slightly, depending on your version of Windows.