python
53 lines · 8 steps
How a Django settings module is wired
A settings file assembles paths, secrets, apps, middleware, and the database from code and environment variables.
Explained by
highlit
1import os
2from pathlib import Path
3
4BASE_DIR = Path(__file__).resolve().parent.parent.parent
5
6SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
7
8INSTALLED_APPS = [
9 "django.contrib.admin",
10 "django.contrib.auth",
11 "django.contrib.contenttypes",
12 "django.contrib.sessions",
13 "django.contrib.messages",
14 "django.contrib.staticfiles",
15 "rest_framework",
16 "accounts",
17 "billing",
18]
19
20MIDDLEWARE = [
21 "django.middleware.security.SecurityMiddleware",
22 "django.contrib.sessions.middleware.SessionMiddleware",
23 "django.middleware.common.CommonMiddleware",
24 "django.middleware.csrf.CsrfViewMiddleware",
25 "django.contrib.auth.middleware.AuthenticationMiddleware",
26 "django.contrib.messages.middleware.MessageMiddleware",
27]
28
29ROOT_URLCONF = "config.urls"
30WSGI_APPLICATION = "config.wsgi.application"
31
32DATABASES = {
33 "default": {
34 "ENGINE": "django.db.backends.postgresql",
35 "NAME": os.environ["POSTGRES_DB"],
36 "USER": os.environ["POSTGRES_USER"],
37 "PASSWORD": os.environ["POSTGRES_PASSWORD"],
38 "HOST": os.environ.get("POSTGRES_HOST", "localhost"),
39 "PORT": os.environ.get("POSTGRES_PORT", "5432"),
40 }
41}
42
43AUTH_USER_MODEL = "accounts.User"
44
45LANGUAGE_CODE = "en-us"
46TIME_ZONE = "UTC"
47USE_I18N = True
48USE_TZ = True
49
50STATIC_URL = "static/"
51STATIC_ROOT = BASE_DIR / "staticfiles"
52
53DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Deriving BASE_DIR from __file__ makes every path relative to the project, not the working directory.
- 2Reading secrets and credentials from os.environ keeps them out of source control and lets deploys differ safely.
- 3Django assembles its behavior from ordered lists and dotted string paths that it imports lazily at startup.
Related explainers
python
from fastapi import FastAPI, WebSocket, WebSocketDisconnect app = FastAPI()
Building a WebSocket chat with FastAPI
websockets
broadcast
connection-management
Intermediate
9 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
python
import random from typing import Iterator, List
How reservoir sampling picks k items
reservoir-sampling
streaming
randomness
Intermediate
5 steps
python
import secrets from django.contrib.auth import authenticate, login from django.core.cache import cache
Two-factor login with OTP in Django
two-factor-auth
one-time-passwords
caching
Intermediate
9 steps
go
package middleware import ( "net/http"
Per-plan export limits in Gin middleware
middleware
rate-limiting
authorization
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-django-settings-module-is-wired-explained-python-9b7a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.