Django: project structure and settings (settings.py)

Lesson 3 of 9 · Python web: Django project

The file settings.py holds everything that determines how the project works: which apps are included, where the templates and static files are, which language and time zone, which database. In this lesson we set it up for the Logo creator and make a folder for the templates.

Before you start: required prior knowledge – project and apps.
How to learn: work in your own project step by step and after each lesson check that the server runs; do the task at the end on your own first. The code is for Django 5.2 (LTS) and Python 3.12+; write each part into the file named in the comment at the top of the example.

Project files

FileRole
manage.pycommands: runserver, migrate, startapp, createsuperuser, shell…
logo_kreator/settings.pysettings (this lesson)
logo_kreator/urls.pythe main map of addresses (lesson 4)
logo_kreator/wsgi.py, asgi.pythe entry point for a server on the internet (Gunicorn – lesson 9)
logoi/models.py, views.py, admin.pythe app's models, views and admin
logoi/migrations/the history of database changes
requirements.txtthe list of the project's packages (we make it below)

Changes in settings.py

# logo_kreator/settings.py – changes compared with the file created by startproject

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    # our apps
    "logoi",
    "nalozi",
]

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / "templates"],      # shared templates (base.html, registration/...)
        "APP_DIRS": True,                      # and the templates from each app's templates/ folder
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

LANGUAGE_CODE = "en-us"            # Django's messages (forms, admin) in English
TIME_ZONE = "Europe/Belgrade"
USE_I18N = True
USE_TZ = True

STATIC_URL = "static/"

# logging in and out (lesson 7)
LOGIN_URL = "login"
LOGIN_REDIRECT_URL = "moji_logoi"
LOGOUT_REDIRECT_URL = "naslovna"
SettingWhy
INSTALLED_APPSDjango "knows" only about the included apps: their models, templates, admin
TEMPLATES → DIRSthe templates folder in the project root for shared templates (base.html, the login pages)
LANGUAGE_CODE = "en-us"form errors and the admin panel in English (for Serbian in Latin script: "sr-latn")
TIME_ZONE, USE_TZtime is stored in UTC and shown in local time (here Belgrade time – write your own zone)
STATIC_URLthe address under which CSS, JS and images are served: /static/...
LOGIN_*where logging in and logging out lead (lesson 7)

Also make empty folders for the templates: templates/ in the root, templates/registration/, logoi/templates/logoi/ and nalozi/templates/nalozi/. The app name is repeated in the path (logoi/templates/logoi/) so that the templates of two apps with the same file name do not get mixed up.

DEBUG and SECRET_KEY

SettingDevelopmentOn the internet
DEBUGTrue – a detailed error page with codealways False – the error details would reveal the code and the settings
SECRET_KEYthe value from startprojecta secret from an environment variable, never in code on GitHub
ALLOWED_HOSTSempty (localhost works)the site's domain name

How this is set up for a server – in the lesson Putting the site on the internet.

Database: SQLite and PostgreSQL

# the default (already in settings.py) – SQLite, a single file in the project
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}

SQLite is enough for development and for small sites. On a server PostgreSQL is usually used; then, instead of this block, the database address is read from an environment variable (lesson 9). The code of the models and views does not change – Django takes care of the differences.

requirements.txt

pip freeze > requirements.txt
:: on another computer (or on the server):
pip install -r requirements.txt

The file contains the exact package versions, e.g. Django==5.2.8. Run pip freeze again every time you install a new package.

Practice tasks

Each task has an example, a hint and a solution.

TASK 01Difficulty: medium

What is wrong?

A student wrote INSTALLED_APPS = [..., "Logoi"] in settings.py, left DEBUG = True on the server and put the SECRET_KEY in a public GitHub repository. Name three problems and how they are solved.

Common mistakes and tips
  • After changing INSTALLED_APPS run python manage.py check – it reports an error before you notice it in the browser.
  • The templates folder in DIRS is in the project root, next to manage.py – not inside logo_kreator/.
  • Do not copy a whole settings.py from the internet – change only the parts you need.

Check your knowledge

Open the quiz

Project settings

1. What must DEBUG be on a site available to everyone?



2. Where do you write that the project uses the app logoi?




3. What is requirements.txt for?