The Settings File
settings.py holds every piece of project configuration.
The settings.py module is the control center of your project. It is a normal Python file, so values can be computed at runtime.
Important settings
DEBUG—Truein development, must beFalsein production.ALLOWED_HOSTS— the domains allowed to serve your site.INSTALLED_APPS— the list of apps Django loads.DATABASES— database connection settings.SECRET_KEY— used for cryptographic signing; keep it private.
Example
# mysite/settings.py (excerpt)
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"blog", # your own app
]When to use it
- A developer sets DEBUG=False and ALLOWED_HOSTS before deploying the Django app to a staging server.
- A team splits settings into base.py, local.py, and production.py to share common config while isolating secrets.
- A developer adds a new database backend by updating the DATABASES dictionary in settings.py.
More examples
Core settings overview
Shows the most important settings every Django project configures: debug mode, hosts, apps, and database.
DEBUG = True
ALLOWED_HOSTS = ["*"]
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"myapp",
]
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}Switch to PostgreSQL
Switches the database backend to PostgreSQL by filling in the DATABASES dictionary.
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "mydb",
"USER": "myuser",
"PASSWORD": "secret",
"HOST": "localhost",
"PORT": "5432",
}
}Split settings per environment
Demonstrates splitting settings across files so local and production environments share a common base.
# settings/base.py
INSTALLED_APPS = [...]
# settings/local.py
from .base import *
DEBUG = True
# settings/production.py
from .base import *
DEBUG = False
ALLOWED_HOSTS = ["example.com"]
Discussion