· 6 years ago · Jul 20, 2019, 05:42 PM
1def ensure_secret_key_file():
2 """Checks that secret.py exists in settings dir. If not, creates one
3 with a random generated SECRET_KEY setting."""
4 secret_path = os.path.join(BASE_DIR, 'secret.py')
5 if not os.path.exists(secret_path):
6 from django.utils.crypto import get_random_string
7 secret_key = get_random_string(
8 50, 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
9 with open(secret_path, 'w') as f:
10 f.write("SECRET_KEY = " + repr(secret_key) + "\n")