Skip to content

Instantly share code, notes, and snippets.

@billmei
Forked from ndarville/secret-key-gen.py
Last active December 22, 2015 13:28
Show Gist options
  • Save billmei/6479195 to your computer and use it in GitHub Desktop.
Save billmei/6479195 to your computer and use it in GitHub Desktop.
import os
"""
Two things are wrong with Django's default `SECRET_KEY` system:
1. It is not random but pseudo-random
2. It saves and displays the SECRET_KEY in `settings.py`
This snippet
1. uses base 64 encode instead to generate a random key
2. gets an environment variable `DJANGO_SECRET_KEY`
The result is a random and safely hidden `SECRET_KEY`.
"""
try:
SECRET_KEY
except NameError:
SECRET_FILE = os.path.join(PROJECT_DIR, 'secret.txt')
try:
SECRET_KEY = open(SECRET_FILE).read().strip()
except IOError:
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment