Generate SECRET_KEY
Uses a Django-style character set by default. Store it in .env and never commit it to Git.
Leave empty to use the preset charset. If you provide your own, it will be used for generation.
How to use
Store SECRET_KEY in .env and load it in settings.py to avoid leaks.
import os
SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY", "")
if not SECRET_KEY:
raise RuntimeError("DJANGO_SECRET_KEY is not set")
Security notes
- If SECRET_KEY is leaked, rotate it immediately. Rotation can invalidate existing sessions and signed data.
- Never ship SECRET_KEY in frontend code or public repos. Use environment variables or a secret manager.
- Use different SECRET_KEY values per project and environment.
About Django SECRET_KEY
In Django, SECRET_KEY is used for cryptographic signing features (e.g., sessions, CSRF, password reset tokens). It is not your database password, but it must be unpredictable and kept secret.