Topic · Security & configuration

Settings & secrets for Django in production

Production settings, environment variables, and secrets, and the errors Django raises when they are wrong.

secrets reach settings, never the repositorySecret storeor env file, 0600Git repositorynever✕Process envsystemd, containersettings.pyos.environ[...]Code decides the shape of the settings; the environment supplies the values

Production settings and secrets decide whether Django is safe and whether it starts at all. Many production-only errors, from DisallowedHost to CSRF failures, are settings problems.

The guides cover structuring settings, loading secrets from the environment or a secret store, rotating keys, and fixing the errors that misconfiguration causes.

16Set it up
6Run it well
3Check it
25Fix it

Set it up

Guides that take this from nothing to running in production.

  • Configure Django Environment Variables for Production

    Read Django settings from environment variables safely: typed parsing, fail-fast secrets, systemd and Docker env files, and verification.

  • Manage Django Secrets in Production

    Store, load, and rotate Django secrets: inventories, file-based secrets, systemd credentials, cloud secret managers, and SECRET_KEY_FALLBACKS.

  • Create a Django Superuser in Production Without a Shell

    Create the first Django admin user in production non-interactively: createsuperuser --noinput with DJANGO_SUPERUSER_ environment variables, one-off containers and Jobs, PaaS consoles, an idempotent management command, and securing the account afterwards.

  • Split Django Settings for Development and Production

    Organise Django settings for development, staging, and production: a settings package with base, dev, and prod modules, environment variables for secrets, DJANGO_SETTINGS_MODULE per environment, and tests that keep production settings safe.

  • Configure Django Settings With django-environ

    Use django-environ to read Django settings from environment variables and .env files: typed values for DEBUG and ALLOWED_HOSTS, DATABASE_URL and CACHE_URL parsing, required variables, safe .env handling, and when to skip .env in production.

  • Load Django Secrets From AWS Secrets Manager

    Load Django secrets such as SECRET_KEY and database passwords from AWS Secrets Manager: inject them into ECS and EKS as environment variables, fetch them at startup with boto3 and caching, grant least-privilege IAM, and rotate RDS credentials.

  • Load Django Secrets From HashiCorp Vault

    Use HashiCorp Vault for Django secrets: read KV secrets with hvac using AppRole or Kubernetes auth, render them with Vault Agent or the Vault Secrets Operator, issue dynamic PostgreSQL credentials, and handle lease renewal and restarts.

  • Encrypt Django Secrets in Git With SOPS

    Keep Django environment secrets encrypted in the repository with Mozilla SOPS and age: encrypt .env and YAML files, decrypt during deploys with Ansible or CI, use cloud KMS keys for teams, and keep keys out of Git.

  • Manage Django Secrets With Doppler

    Manage Django environment variables and secrets with Doppler: organize projects and configs per environment, inject secrets with doppler run locally and on servers, sync to Heroku, Render, Vercel, or Kubernetes, use service tokens in CI, and rotate safely.

  • Manage Django Secrets With Infisical

    Use Infisical, an open-source secrets manager, for Django: organize projects and environments, inject secrets with infisical run, authenticate servers and CI with machine identities, sync to Kubernetes and platforms, and self-host or use Infisical Cloud.

  • Use Google Secret Manager With Django

    Load Django secrets from Google Cloud Secret Manager: mount secrets as environment variables or files on Cloud Run, read them with the Python client on Compute Engine and GKE, grant least-privilege access to service accounts, and rotate secrets with versions.

  • Use Azure Key Vault With Django

    Load Django secrets from Azure Key Vault: use Key Vault references in App Service and Container Apps, read secrets with azure-identity and managed identities, mount them on AKS with the Secrets Store CSI driver, and rotate values without redeploying code.

  • Load Django Settings From AWS SSM Parameter Store

    Use AWS Systems Manager Parameter Store for Django configuration and secrets: store SecureString parameters under a path per environment, inject them into ECS task definitions, read them with boto3 on EC2 and Lambda, grant least-privilege IAM, and compare with Secrets Manager.

  • Serve Django Under a URL Prefix (/app) Behind Nginx

    Run Django under a subpath such as example.com/app behind Nginx: forward SCRIPT_NAME correctly, set FORCE_SCRIPT_NAME, STATIC_URL, and cookie paths, and avoid broken admin links, redirects, and CSRF errors.

  • Use Docker Secrets With Django

    Pass Django secrets to containers as files instead of environment variables: Docker Compose and Swarm secrets mounted at /run/secrets, the _FILE convention, reading them in settings, and keeping secrets out of images and docker inspect.

  • Sync Django Secrets Into Kubernetes With External Secrets Operator

    Use External Secrets Operator to load Django secrets from AWS Secrets Manager, Google Secret Manager, Azure Key Vault, Vault, or Infisical into Kubernetes Secrets: configure a SecretStore with workload identity, map keys, set refresh intervals, and roll pods on change.

Run it well

Tuning, reliability, and operations once it works.

  • Configure Django Sessions for Production

    Choose and tune Django's session backend for production: database, cached_db, cache, or signed cookies; cookie security flags; expiry; write frequency; and cleaning up expired sessions.

  • Custom 404 and 500 Error Pages for Django in Production

    Create custom 404, 500, 403, and 400 pages for a Django site in production: templates and handler views, a 500 page that cannot fail, static assets that load, Nginx error pages for when Django is down, and testing them with DEBUG off.

  • Rotate the Django Database Password Without Downtime

    Rotate PostgreSQL or MySQL credentials used by a Django app without outages: use two database roles or dual passwords, update secrets in the right order, restart web, Celery, and cron processes, handle PgBouncer, and automate rotation with Secrets Manager.

  • Rotate the Django SECRET_KEY Safely

    Rotate Django's SECRET_KEY without breaking sessions and password reset links: what the key signs, SECRET_KEY_FALLBACKS, a staged rollout, leak response, and third-party libraries that use the key.

  • Manage Django Settings on Kubernetes With ConfigMaps and Secrets

    Deliver Django configuration on Kubernetes: ConfigMaps for non-secret settings, Secrets for credentials, envFrom versus mounted files, rolling pods when values change, encryption at rest, and syncing from external secret managers.

  • What to Do When Django Secrets Leak

    Respond to a leaked Django SECRET_KEY, database password, or API key: assess exposure, rotate in the right order, use SECRET_KEY_FALLBACKS only when appropriate, invalidate sessions and tokens, purge Git history, and prevent the next leak.

Check it

Review lists to run before launch or after a change.

Fix it

Errors and symptoms, each traced to a cause and a fix.

Learning paths through this topic

Related topics