settings.py (Django)

settings.py is the configuration file for a Django project. It defines the database, installed apps, middleware, templates, static files, secret key, and many other options that control how the whole site behaves.

Django

Everything Django needs to run is declared here. Key settings include INSTALLED_APPS (the apps in your project), DATABASES (connection details), MIDDLEWARE (the request/response processing chain), TEMPLATES, STATIC_URL, SECRET_KEY, DEBUG, and ALLOWED_HOSTS.

Because settings.py is plain Python, you can compute values and read environment variables. In real projects you keep secrets (SECRET_KEY, database passwords, API keys) out of the file by loading them from the environment, often with a library like python-decouple or django-environ.

A common production setup splits settings into base/dev/prod modules or branches on an environment variable, so DEBUG is True locally but False in production with proper ALLOWED_HOSTS and security settings.

Syntax

INSTALLED_APPS = [...]
DATABASES = {...}
SECRET_KEY = os.environ["SECRET_KEY"]

Examples

Python

import os

DEBUG = os.environ.get("DEBUG") == "1"
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
ALLOWED_HOSTS = ["example.com"]

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "blog",
]

Learn it in a lesson

See also

Back to the Dictionary