✅
Security

Django Security Checklist: What to Verify Before Going to Production

23.11.2026
← All articles

Django comes with CSRF protection, auto-escaping templates, an ORM that prevents SQL injection and strong password hashing. Much of that depends on configuration, though, and the default settings.py from startproject is built for development. Work through this list before launch.

Example: a freight company's client portal

A freight company in Fergana is launching a Django portal at yuk.example.uz where clients track shipments and download contracts and invoices. That data interests competitors and fraudsters alike, so keep it in mind for each item. A single misconfigured setting could expose a client's invoices to anyone who guesses the right URL.

1. Run Django's deployment check

python manage.py check --deploy --settings=config.settings.production

It flags most of the issues below. Resolve each warning or document why you accept it, and make the command part of every deployment.

2. Turn DEBUG off

With DEBUG = True, error pages show settings, request data, source snippets and URL patterns. Django masks settings that look secret, but custom variables with other names may slip through, and debug mode also keeps every SQL query in memory. Read the value from the environment and keep it False. With debug off, also set ADMINS and logging so that server errors still reach you by email or in a log file instead of disappearing silently.

3. Keep SECRET_KEY out of the code

The key signs sessions, password reset links and CSRF tokens. Anyone who sees it in a repository can forge signatures. Store it in an environment variable and use a different key per environment.

# settings/production.py
import os

DEBUG = False
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
ALLOWED_HOSTS = ["yuk.example.uz"]
CSRF_TRUSTED_ORIGINS = ["https://yuk.example.uz"]

Rotating the key logs users out; recent versions offer SECRET_KEY_FALLBACKS for a smooth transition.

4. ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS

ALLOWED_HOSTS defends against Host header attacks such as poisoned password reset links; a wildcard disables that. Since Django 4.0, entries in CSRF_TRUSTED_ORIGINS must include the scheme.

5. HTTPS and secure cookies

SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_HSTS_SECONDS = 3600

Only set the proxy header if Nginx actually sets it and strips any client-supplied copy. Start HSTS small and raise it once everything works, because browsers remember it. On sayt.uz hosting the free Let's Encrypt certificate renews automatically; on a VPS you configure renewal yourself.

6. Admin URL and accounts

Move the admin away from /admin/, for example path("boshqaruv-k7m2/", admin.site.urls), use strong passwords, limit superusers and add login attempt throttling.

7. Static and media files

Collect static files and serve them with Nginx. Validate type and size of uploads, make sure nothing executes in the media folder, and serve private documents through a view that checks permissions, so a guessed file path never bypasses the login.

8. Server and dependencies

Run Gunicorn or uWSGI as an unprivileged user bound to localhost, give the database user only the rights it needs, stay on a supported Django release and audit packages:

pip install pip-audit
pip-audit -r requirements.txt
gunicorn config.wsgi:application --bind 127.0.0.1:8000 --workers 3

Finally, use parameters in raw SQL, reserve |safe and mark_safe for trusted text, and configure logging so you hear about errors. Revisit this list after every major upgrade, since new Django releases sometimes add or change security settings.

Related articles

🌐 The danger of misconfigured CORS: how to open your API correctly 🔍 Scanning a Website with ClamAV: Setup, Commands and Reading the Results 🦠 Malware on Your PC Steals FTP Passwords: Signs and Protection 📤 Securing a File Upload Form: Validation, Storage and Delivery
🌐 Language
🇺🇿 O⁠'zbek 🇺🇿 Özbek (yangi) 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓