What Django uses SECRET_KEY for
Django uses SECRET_KEY to sign session cookies, password reset tokens, messages and anything passed through its signing framework. If it leaks, an attacker can forge those signatures. If it changes, existing sessions and reset links stop working.
This generator uses the same character set and length as Django’s own get_random_secret_key(): 50 characters from lowercase letters, digits and the symbols !@#$%^&*(-_=+). That gives about 282 bits of entropy.
Keep it out of settings.py
Load the key from the environment instead of committing it to your repository:
import os
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
Choose the .env output to get a line you can paste into an environment file. The value is wrapped in single quotes because characters like $ and # have special meaning in shells and dotenv parsers.
Frequently asked questions
What happens if I change the SECRET_KEY?
Users are logged out and outstanding password reset links stop working. Since Django 4.1 you can list old keys in SECRET_KEY_FALLBACKS to rotate without logging everyone out.
Can I use the same key for development and production?
No. Use a different key for each environment, and never use a key in production that has been committed to Git.