SECRET_KEY, DEBUG, and cookie security settings
coreintermediateSECRET_KEY signs sessions and tokens and must stay secret; DEBUG must be False in production; ALLOWED_HOSTS/CSRF_TRUSTED_ORIGINS reject requests claiming the wrong host; the SESSION_COOKIE_*/CSRF_COOKIE_* settings control how those two cookies can be read, sent, and reused cross-site.
Think of it as
SECRET_KEY is the master signature Django stamps on every session and CSRF token — leak it, and an attacker can forge both. DEBUG is a door left open for developers to see full stack traces, deliberately propped shut in production. ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS are guest lists checked before a request is trusted at all; the SESSION_COOKIE_*/CSRF_COOKIE_* pairs are the fine print on the cookies themselves — who can read them (HTTPONLY), when they're sent (SECURE, SAMESITE).
What we're doing: Configure ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS together for a production site served over HTTPS behind a real domain.
- 1
- ALLOWED_HOSTS checks the Host header on every request — a mismatch returns a 400 before any view runs.
- 2
- CSRF_TRUSTED_ORIGINS is separate and requires a full scheme (https://) — it governs which Origin/Referer a POST is trusted from, not which Host is served.
Why this works: ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS solve two different attacks — Host header injection (cache poisoning, password-reset link poisoning) and cross-site request forgery — and Django deliberately keeps them as separate settings rather than inferring one from the other, since a legitimate reverse-proxy setup can need different values for each.
Setting DEBUG = False without ever configuring ALLOWED_HOSTS
Wrong
Better
What you see: Every single request returns "Bad Request (400)" — the site appears completely down, with no view ever running.
Why: ALLOWED_HOSTS defaults to an empty list, and Django enforces the check specifically when DEBUG is False (as a safety net, since DEBUG = True skips it) — an empty list matches nothing, so every request's Host header fails validation before routing even starts.
- SECRET_KEY / DEBUG — fail OPEN toward insecurity if forgotten
- ALLOWED_HOSTS — fails CLOSED — empty means every request 400s
SECRET_KEY, DEBUG, host, and cookie security settings
Together
Remember: SECRET_KEY must never be committed and DEBUG must be False in production — both fail open toward insecurity if forgotten, unlike ALLOWED_HOSTS, which fails closed (400s everything) if left empty.
See also: sessions · https and hsts settings · configuration strategy

