The settings.py module is the command center of every Django project. It dictates how your application connects to databases, authenticates sessions, renders templates, and enforces security. Understanding how to configure settings cleanly — and separating secrets from version control — is a non-negotiable standard for professional engineering.
| Setting | Purpose & Mechanism | Production Requirement |
|---|---|---|
| SECRET_KEY | Used to sign cryptographic cryptographic hashes, session cookies, password reset tokens, and CSRF tokens. | Keep secret! 50+ character random string loaded from environment. |
| DEBUG | Controls detailed stack trace error pages and automatic serving of local static files. | MUST BE FALSE! Never deploy with DEBUG = True. |
| ALLOWED_HOSTS | List of domain names and IP addresses your Django app is authorized to serve. | Explicit domain names only: ['veewom.com', 'www.veewom.com']. |
| DATABASES | Dictionary defining active database engines, credentials, hosts, and connection pools. | Production RDBMS (e.g. PostgreSQL) instead of local SQLite file. |
| MIDDLEWARE | Linear stack of request/response interceptors executed in strict sequential order. | SecurityMiddleware, WhiteNoise, and SessionMiddleware active. |
Hardcoding secrets into settings.py causes critical security breaches when code is pushed to public or private git repositories. The industry standard is to use django-environ to read configurations from a local .env file:
# Install django-environ
pip install django-environ
Create a .env file in your project root (and ensure it is added to .gitignore):
# .env file (DO NOT COMMIT TO GIT)
DEBUG=True
SECRET_KEY=django-insecure-9^g!2x#km*!81_v@880e4f_enterprise_key_2026
DATABASE_URL=sqlite:///db.sqlite3
ALLOWED_HOSTS=localhost,127.0.0.1
Now, refactor your settings.py to load values dynamically:
# mywebsite/settings.py
import environ
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Initialize environ instance
env = environ.Env(
DEBUG=(bool, False)
)
# Read .env file if it exists
environ.Env.read_env(BASE_DIR / '.env')
# Safe, decoupled settings
SECRET_KEY = env('SECRET_KEY')
DEBUG = env('DEBUG')
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS')
# Database configuration via URL schema
DATABASES = {
'default': env.db('DATABASE_URL', default=f"sqlite:///{BASE_DIR / 'db.sqlite3'}")
}
Django includes a built-in automated deployment security auditor. Before pushing code to production, always run: python manage.py check --deploy. It audits your settings and warns if cookies, HSTS headers, or secret keys are insecure!
Broaden your backend architecture, REST API design, and production engineering expertise:
Production security checklists: SECRET_KEY, SSL, and debug protection.
Configure STATIC_ROOT, STATIC_URL, and STATICFILES_DIRS.
Configure CACHES in settings.py for lightning-fast page loading.
Deploy production settings securely using containerized environments.