Django: Check Security Before Deploying with manage.py check –deploy in 5 Minutes
Before shipping a Django project to production, run python manage.py check –deploy: it scans your settings file and lists security holes such as DEBUG = True, a SECRET_KEY that is too short, missing HTTPS, or cookies wit
Before shipping a Django project to production, run python manage.py check –deploy: it scans your settings file and lists security holes such as DEBUG = True, a SECRET_KEY that is too short, missing HTTPS, or cookies without the Secure flag. The command ships with every Django project and finishes in seconds — but it is only accurate when run against your production settings. Running it on dev settings gives misleading results.
The Problem
Production settings mistakes rarely crash the app, so they slip through easily. A freshly created project, deployed as-is without a settings review, carries a whole set of dev defaults: DEBUG = True, a short default SECRET_KEY, an empty ALLOWED_HOSTS, and missing security middleware. The result: error pages that expose tracebacks and environment variables, session cookies sent over plain HTTP, and browsers that do not block your pages from being embedded in a stranger’s iframe.
# settings.py — dev config shipped straight to production
SECRET_KEY = "short-key"
DEBUG = True
ALLOWED_HOSTS = []
MIDDLEWARE = [
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
]
Run the check against the correct production settings file:
python manage.py check --deploy --settings=myproject.settings_prod
With the dev settings above left untouched, Django 6.1 returns 7 warnings:
WARNINGS:
?: (security.W001) You do not have 'django.middleware.security.SecurityMiddleware' in your MIDDLEWARE so the SECURE_HSTS_SECONDS, SECURE_CONTENT_TYPE_NOSNIFF, SECURE_REFERRER_POLICY, SECURE_CROSS_ORIGIN_OPENER_POLICY, and SECURE_SSL_REDIRECT settings will have no effect.
?: (security.W002) You do not have 'django.middleware.clickjacking.XFrameOptionsMiddleware' in your MIDDLEWARE, so your pages will not be served with an 'x-frame-options' header.
?: (security.W009) Your SECRET_KEY has less than 50 characters, less than 5 unique characters, or it's prefixed with 'django-insecure-' indicating that it was generated automatically by Django.
?: (security.W012) SESSION_COOKIE_SECURE is not set to True. Using a secure-only session cookie makes it more difficult for network traffic sniffers to hijack user sessions.
?: (security.W016) You have 'django.middleware.csrf.CsrfViewMiddleware' in your MIDDLEWARE, but you have not set CSRF_COOKIE_SECURE to True.
?: (security.W018) You should not have DEBUG set to True in deployment.
?: (security.W020) ALLOWED_HOSTS must not be empty in deployment.
System check identified 7 issues (0 silenced).
The Solution
Fix each group of warnings: enable the security middleware, turn off DEBUG, set a long key, enforce HTTPS and Secure cookies. Generate a fresh key with get_random_secret_key(), then read it from an environment variable instead of hardcoding it:
import os
SECRET_KEY = os.environ["SECRET_KEY"]
DEBUG = False
ALLOWED_HOSTS = ["example.com"]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
Run the check again and Django reports a clean bill of health:
System check identified no issues (0 silenced).
Two things to note. First, the HSTS (W004) and SSL redirect (W008) warnings only appear after you have added SecurityMiddleware — without it, Django reports only W001 and skips that whole group. Second, only enable HSTS once the entire site runs stably on HTTPS, because browsers remember the policy and force HTTPS for the duration you set; the Django docs warn that enabling HSTS carelessly can have serious, hard-to-reverse consequences.
Apply It to Your Project
Add this command to your CI/CD pipeline, run it before every deploy, and always point it at the right production settings file with –settings or the DJANGO_SETTINGS_MODULE variable — running it on dev settings and feeling safe is the most common mistake. Keep dev and prod settings in two separate files, and treat “0 issues” as a precondition before opening the gates to real users.
Related Posts
Django: Write Custom Middleware to Measure Request Time in 5 Minutes
Flask: Add Security Headers to Every Response with after_request in 5 Minutes
FastAPI: cấu hình CORS đúng cách bằng CORSMiddleware trong 5 phút
Frequently Asked Questions
How is check –deploy different from a plain check?
A plain check scans for general configuration errors. The –deploy option enables the extra security.* checks, which only make sense in production — for example DEBUG, SECRET_KEY, HSTS, and Secure cookies.
Can I run check –deploy against dev settings?
You can, but the results are noisy: it flags things that are perfectly fine in dev, such as DEBUG = True or an empty ALLOWED_HOSTS. For results you can act on, always run it with the production settings file via –settings or DJANGO_SETTINGS_MODULE.
If Nginx already redirects to HTTPS, do I still need SECURE_SSL_REDIRECT?
Not necessarily. Warning W008 itself accepts redirecting at the load balancer or reverse proxy. If the proxy handles the redirect, silence that warning alone with SILENCED_SYSTEM_CHECKS = [“security.W008”].
Is there any risk in enabling HSTS?
Browsers cache the HSTS policy for SECURE_HSTS_SECONDS; if HTTPS breaks, users cannot reach the site over HTTP until it expires. Start with a small value, for example 3600 seconds, and raise it gradually once the whole site runs stably on HTTPS.
Does check –deploy replace a security review?
No. The command only scans your settings file — it does not inspect your code, third-party packages, or web server configuration. Treat it as the minimum check, not the whole security process.
💬 Have you tried this in your own project? If you hit an error or have a neater way to do it, leave a comment below — I read them all and will add the best ideas to the post.
References
Django documentation — Deployment checklist: https://docs.djangoproject.com/en/6.1/howto/deployment/checklist/
Django documentation — django-admin check: https://docs.djangoproject.com/en/6.1/ref/django-admin/#check
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.