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 APIRouter, BackgroundTasks, Depends, HTTPException, status from pydantic import BaseModel, EmailStr from sqlalchemy.orm import Session
Building a signup endpoint in FastAPI
dependency-injection
request-validation
background-tasks
Intermediate
8 steps
javascript
const ROLE_PERMISSIONS = { admin: ['users:read', 'users:write', 'billing:read', 'billing:write'], manager: ['users:read', 'billing:read'], member: ['users:read'],
Role-based permissions middleware in Express
authorization
middleware
rbac
Intermediate
9 steps
java
public interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
Intermediate
8 steps
python
from django import forms from django.utils import timezone from .models import Reservation
Multi-field validation in a Django ModelForm
form validation
cross-field validation
modelform
Intermediate
7 steps
python
from django import template from django.urls import reverse, NoReverseMatch from django.utils.html import format_html
Active nav-link template tags in Django
template tags
url routing
active state
Intermediate
7 steps
go
package config import ( "fmt"
A thread-safe config singleton in Go
singleton
concurrency
environment-variables
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.