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.
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.
| File | Role |
|---|---|
manage.py | commands: runserver, migrate, startapp, createsuperuser, shell… |
logo_kreator/settings.py | settings (this lesson) |
logo_kreator/urls.py | the main map of addresses (lesson 4) |
logo_kreator/wsgi.py, asgi.py | the entry point for a server on the internet (Gunicorn – lesson 9) |
logoi/models.py, views.py, admin.py | the app's models, views and admin |
logoi/migrations/ | the history of database changes |
requirements.txt | the list of the project's packages (we make it below) |
# 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"
| Setting | Why |
|---|---|
INSTALLED_APPS | Django "knows" only about the included apps: their models, templates, admin |
TEMPLATES → DIRS | the 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_TZ | time is stored in UTC and shown in local time (here Belgrade time – write your own zone) |
STATIC_URL | the 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.
| Setting | Development | On the internet |
|---|---|---|
DEBUG | True – a detailed error page with code | always False – the error details would reveal the code and the settings |
SECRET_KEY | the value from startproject | a secret from an environment variable, never in code on GitHub |
ALLOWED_HOSTS | empty (localhost works) | the site's domain name |
How this is set up for a server – in the lesson Putting the site on the internet.
# 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.
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.
Each task has an example, a hint and a solution.
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.
INSTALLED_APPS run python manage.py check – it reports an error before you notice it in the browser.templates folder in DIRS is in the project root, next to manage.py – not inside logo_kreator/.settings.py from the internet – change only the parts you need.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?