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

  • DEBUGTrue in development, must be False in 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

Example · python
# 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.

Example · python
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.

Example · python
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.

Example · python
# 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

  • Be the first to comment on this lesson.