Full answer
These three settings cause many real production incidents.
DEBUG
- With
DEBUG=True, an unhandled error shows a page with the stack trace, local variables and settings (with some values hidden). That is useful locally and dangerous in public. - With
DEBUG=False, Django shows a plain 500 page (or your500.html) and emails or logs the error throughLOGGING/ADMINS. - DEBUG also changes behaviour:
runserverstops serving static files, and SQL queries are no longer recorded inconnection.queries.
ALLOWED_HOSTS
- A list such as
["portal.example.edu", ".example.edu"](a leading dot allows subdomains). - Django validates the request's
Hostheader against it wheneverrequest.get_host()is called, whichCommonMiddleware, CSRF checks and URL building all do, and returns 400 for other hosts. This stops attackers from making Django build links (password reset emails, absolute URLs) with a host they control. - When
DEBUG=Trueand the list is empty, Django allows localhost names only.
SECRET_KEY
- Used for signing: sessions with signed cookies, password reset tokens,
django.core.signing, messages. - If it leaks, attackers can forge signed data. If you change it, existing sessions and reset links become invalid. Django 4.1+ supports
SECRET_KEY_FALLBACKSfor rotating keys without logging everyone out.
Load secrets from the environment (os.environ["DJANGO_SECRET_KEY"]) or a secrets manager, not from settings.py in Git. python manage.py check --deploy warns about insecure values.
python
import os
os.environ.setdefault("DJANGO_SECRET_KEY", "x7#k2-demo-only-not-for-production-0c9f1e5b8a7d6c4e3f2a1b")
os.environ.setdefault("DJANGO_ALLOWED_HOSTS", "portal.example.edu")
import django
from django.conf import settings
settings.configure(
DEBUG=os.environ.get("DJANGO_DEBUG", "0") == "1",
SECRET_KEY=os.environ["DJANGO_SECRET_KEY"],
ALLOWED_HOSTS=os.environ["DJANGO_ALLOWED_HOSTS"].split(","),
ROOT_URLCONF=__name__,
MIDDLEWARE=["django.middleware.common.CommonMiddleware"],
INSTALLED_APPS=[],
)
django.setup()
from django.core import signing
from django.http import HttpResponse
from django.test import Client
from django.urls import path
urlpatterns = [path("", lambda r: HttpResponse("home"))]
print("DEBUG:", settings.DEBUG)
c = Client()
print("good host:", c.get("/", HTTP_HOST="portal.example.edu").status_code)
print("bad host:", c.get("/", HTTP_HOST="evil.example").status_code)
token = signing.dumps({"student": 7})
print("signed data round trip:", signing.loads(token))
try:
signing.loads(token[:-2] + "xx")
except signing.BadSignature:
print("tampered token rejected")
Standalone script. It reads settings from environment variables the way a production settings.py should. The demo key is for this example only. Django logs a 'DisallowedHost' warning to stderr for the bad host, which is expected. Output (ran twice with identical results; Python 3.13.5, Django 5.2.20, djangorestframework 3.18.3): DEBUG: False good host: 200 bad host: 400 signed data round trip: {'student': 7} tampered token rejected
Key points
- DEBUG must be False in production
- ALLOWED_HOSTS validates the Host header
- SECRET_KEY signs sessions and tokens; keep it secret
- SECRET_KEY_FALLBACKS allows key rotation
- check --deploy flags unsafe settings
Common mistakes
- Deploying with DEBUG=True
- Setting ALLOWED_HOSTS = ['*'] in production
- Committing SECRET_KEY to a public repository
- Rotating SECRET_KEY without SECRET_KEY_FALLBACKS and logging every user out
Follow-ups
- What would an attacker do with a leaked SECRET_KEY?
- How do you manage different settings for local, staging and production?
- What else does check --deploy warn about?
Tips
- Explain the risk of each setting, not just its purpose
- Mention environment variables or a secrets manager
- Bring up manage.py check --deploy