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.

bash/zsh
export DJANGO_SECRET_KEY='w=#5cb$au3$t+hyj(f7ejgrk7$xet7_q@8m)5qd*c_*)nl1shwr'
bash/zsh
printenv DJANGO_SECRET_KEY  # To check it worked
bash/zsh
w#5cb$au3$t+hyj(f7ejgrk7$xet7_q@8m)5qd*c_*)nl1shwr
bash/zsh
export DJANGO_SECRET_KEY='w=#5cb$au3$t+hyj(f7ejgrk7$xet7_q@8m)5qd*c_*)nl1shwr'
bash/zsh
printenv DJANGO_SECRET_KEY  # To check it worked
bash/zsh
w#5cb$au3$t+hyj(f7ejgrk7$xet7_q@8m)5qd*c_*)nl1shwr
  1. Right-click the Computer icon and choose Properties, or in Windows Control Panel, choose System.

  2. Choose Advanced system settings.

Windows System Properties
  1. On the Advanced tab, click Environment Variables.

Windows Environment Variables
  1. Click New to create a new environment variable.

  2. 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.