A production Django environment needs private secrets, explicit host settings and controlled error output. Development convenience settings should not be carried over without review.
IN THIS ARTICLE
Check access and requirements
Identify the selected settings module and supported Django/Python versions. Prepare runtime secrets through the account environment or another protected configuration mechanism.
01 Set Django production settings and secret keys
Set
DEBUG=Falseand configure the intendedALLOWED_HOSTS. Keep the production secret key private and separate from development credentials.Configure database, cache and email services for production. Use a distinct database and credentials for staging to prevent test jobs from reaching live customer data.
Run
python manage.py check --deployagainst the production settings. Read each warning in the context of the actual HTTPS and proxy configuration.Set appropriate secure cookie and transport controls after confirming HTTPS works consistently. Plan HSTS carefully because browsers retain it beyond a single request.
Restart the app and any workers with the same intended settings. Record where secrets are supplied without writing their values in the handover document.
02 Example: fail closed when the Django secret is missing
In your project’s production settings, load the key from protected runtime configuration and keep debug output disabled. This fragment deliberately refuses to start with a missing secret instead of falling back to a predictable example key.
import os
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
DEBUG = False
ALLOWED_HOSTS = ['example.com', 'www.example.com']
Supply DJANGO_SECRET_KEY through the service’s approved environment mechanism before starting web processes, workers and management commands. Do not place its value in this knowledgebase example or print it during diagnostics. Run the deployment check against the same settings module and review the HTTPS-related findings in the context of your actual proxy.
Expected result: The application starts with its intended private key and shows a controlled public error page. A staging run without the required variable fails clearly, preventing an accidental deployment with a shared fallback secret.
Confirm the expected result
Trigger a harmless missing-page request and verify that no development traceback appears. Test authentication and private service access, then confirm deployment checks remain satisfactory.
Resolve setup problems
If a setting seems ignored, compare the settings module and environment used by the web process with the CLI command. A second environment file or stale worker can cause different behavior.
A common question
Should I generate a new SECRET_KEY on every deployment?
No. Keep a stable production key for ordinary releases. Use a deliberate rotation process and the supported fallback mechanism when rotation is required, accounting for signed data and session effects.