Full answer
Django ships safe defaults for many attacks (auto-escaping, CSRF, parameterised queries, clickjacking protection), but several production settings are off by default because they depend on HTTPS.
check --deploy warns about, among others:
DEBUG = True(W018) and a weak or shortSECRET_KEY(W009).- Empty
ALLOWED_HOSTS(W020). SECURE_SSL_REDIRECT(W008): redirect HTTP to HTTPS. Often handled at the load balancer instead.SECURE_HSTS_SECONDS(W004): sendsStrict-Transport-Security, so browsers refuse plain HTTP for that period. Start small (3600), then raise to a year. AddSECURE_HSTS_INCLUDE_SUBDOMAINSandSECURE_HSTS_PRELOADonly when every subdomain serves HTTPS, because it is hard to undo.SESSION_COOKIE_SECURE(W012) andCSRF_COOKIE_SECURE(W016): cookies sent only over HTTPS.SecurityMiddlewaremissing (W001), andX_FRAME_OPTIONS(W019, defaults to DENY).
Some checks only run when the related middleware or app is present. The HSTS and SSL redirect warnings appear only once SecurityMiddleware is installed, and the session cookie warning only when django.contrib.sessions is installed, so a clean report on a half-configured project can mislead.
Other settings worth setting explicitly: SESSION_COOKIE_HTTPONLY (default True), SECURE_REFERRER_POLICY (default "same-origin"), SECURE_CROSS_ORIGIN_OPENER_POLICY, CSRF_TRUSTED_ORIGINS for cross-origin forms, DATA_UPLOAD_MAX_MEMORY_SIZE and FILE_UPLOAD_MAX_MEMORY_SIZE.
Content Security Policy: Django 6.0 added built-in CSP support (ContentSecurityPolicyMiddleware and SECURE_CSP). On 5.2 use the django-csp package.
Beyond settings:
- Keep Django on a supported release and apply security releases quickly.
- Protect
/admin/with strong passwords, 2FA (django-otp), and rate limiting on login (django-axesor proxy rules). - Use stronger password hashers (Argon2) and
AUTH_PASSWORD_VALIDATORS. - Never build SQL with string formatting; never mark user input safe.
- Scope every queryset by the current user to avoid IDOR.
- Scan dependencies (
pip-audit) in CI and runcheck --deploy --fail-level WARNINGthere too.
import django
from django.conf import settings
settings.configure(
DEBUG=False,
SECRET_KEY="short",
ALLOWED_HOSTS=[],
INSTALLED_APPS=["django.contrib.contenttypes", "django.contrib.auth"],
MIDDLEWARE=[],
)
django.setup()
from django.core import checks
def deploy_warnings():
return sorted(m.id for m in checks.run_checks(include_deployment_checks=True)
if m.id.startswith("security."))
print("no middleware:", deploy_warnings())
settings.MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
print("middleware added:", deploy_warnings())
settings.SECRET_KEY = "k$8f2#mZq9!vR4tL0wXy7^bN3cJ6hD1sPe5gU-aT_iO2rWnE+Hs4Lq" # load from env in real projects
settings.ALLOWED_HOSTS = ["portal.example.edu"]
settings.SECURE_SSL_REDIRECT = True
settings.SECURE_HSTS_SECONDS = 31536000
settings.SECURE_HSTS_INCLUDE_SUBDOMAINS = True
settings.SECURE_HSTS_PRELOAD = True
settings.SESSION_COOKIE_SECURE = True
settings.CSRF_COOKIE_SECURE = True
print("hardened:", deploy_warnings())
Standalone script. It runs Django's deployment checks before and after applying production settings. The SECRET_KEY shown is a demo value; real keys come from the environment. Output (ran twice with identical results; Python 3.13.5, Django 5.2.20, djangorestframework 3.18.3): no middleware: ['security.W001', 'security.W002', 'security.W003', 'security.W009', 'security.W020'] middleware added: ['security.W004', 'security.W008', 'security.W009', 'security.W016', 'security.W020'] hardened: []
Key points
- check --deploy lists missing production settings
- HTTPS redirect, HSTS and secure cookies need HTTPS everywhere
- Roll out HSTS gradually; preload is hard to undo
- Django 6.0 has built-in CSP; use django-csp on 5.2
- Patch quickly, protect the admin, scan dependencies
Common mistakes
- Turning on HSTS preload before every subdomain supports HTTPS
- Setting SECURE_SSL_REDIRECT behind a proxy without SECURE_PROXY_SSL_HEADER, causing redirect loops
- Treating check --deploy as the whole security review
- Leaving the admin on a public URL with no rate limiting or 2FA
Follow-ups
- What does HSTS protect against that a redirect alone does not?
- How would you add a Content Security Policy to a Django 5.2 site?
- How do you protect the Django admin on a public site?
Tips
- Name the settings and what attack each blocks
- Mention the gradual HSTS rollout
- Go beyond settings: patching, admin protection, dependency scanning