Skip to content
devtoolbox.cc

Django · Chapter 1 of 8

Django tutorial: install, create a project and your first app

11 min study · Examples checked with Django 6.1 on Python 3.14 ·

Django is a full web framework: besides routing requests to Python functions, it brings an ORM for databases, a template language for HTML, forms with validation, user accounts, an admin site and security defaults such as CSRF protection. Where FastAPI suits JSON APIs, Django suits server-rendered websites and applications with a lot of data management. This track builds one project over eight chapters, a study catalog of courses and lessons, so each chapter adds to the code of the previous one.

Install Django

Django 6.1 requires Python 3.12 or newer; this track was checked with Python 3.14. Create a folder with a virtual environment (Python setup chapter) and install Django:

python -m pip install "django==6.1.*"
python -m django --version

Pinning the minor version (6.1.*) gets security fixes without surprise upgrades. Django releases a new feature version roughly every eight months, and every third one is a long-term support (LTS) release with three years of security fixes, a good choice for sites you don’t want to upgrade often.

Create a project and an app

django-admin startproject studysite .
python manage.py startapp catalog

The dot after studysite creates the project in the current folder instead of a nested one. You now have:

manage.py            command-line entry point for this project
studysite/           the project package: configuration
    settings.py      installed apps, database, middleware, templates, security
    urls.py          the root URL configuration
    asgi.py, wsgi.py entry points for production servers
catalog/             an app: one feature area
    models.py, views.py, admin.py, apps.py, tests.py, migrations/

A Django project is the whole website and its configuration. An app is a package for one feature area, here the catalog; a larger site might add accounts, billing or blog, and you can also install apps other people wrote. An app only takes part once it is listed in INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "catalog",
]

The django.contrib apps are the built-in admin site, authentication, sessions and so on. Order matters only in a few cases, such as which app’s template wins when two have one with the same name.

A view and its URL

A view is a function that takes an HttpRequest and returns an HttpResponse. Replace catalog/views.py:

from django.http import HttpResponse


def home(request):
    return HttpResponse("<h1>Study catalog</h1><p>Lessons will appear here.</p>")

Give the app its own URL list in a new file, catalog/urls.py:

from django.urls import path

from . import views

app_name = "catalog"
urlpatterns = [
    path("", views.home, name="home"),
]

And include it from the project’s studysite/urls.py:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("catalog.urls")),
]

include hands every URL to the app’s list, so the app doesn’t need to know where it is mounted; mounting it at "courses/" instead would move all its pages without touching the app. app_name and name let code refer to the URL as "catalog:home" instead of hard-coding /, which chapter 2 uses for links and redirects.

Migrate and run the development server

python manage.py migrate
python manage.py runserver

migrate creates the database tables for the installed apps: users, sessions, the admin log and so on, in an SQLite file db.sqlite3 by default. runserver starts a development server at http://127.0.0.1:8000/ that reloads when you change code. Opening that address shows the “Study catalog” heading; /nope shows a 404 page with debugging details; /admin/ shows the admin login, which needs a user created with python manage.py createsuperuser.

python manage.py check validates the configuration without starting anything and is worth running whenever something behaves oddly.

Follow one request

When the browser asks for /, the server passes the request to Django, which runs it through the middleware listed in MIDDLEWARE (security headers, sessions, CSRF checks, authentication). Django then matches the path against ROOT_URLCONF, here studysite.urls, top to bottom. The empty pattern from the catalog app matches, so Django calls home(request). The view returns an HttpResponse, and the middleware gets a chance to adjust it on the way back. If no pattern matches, Django answers 404 itself. Every feature in this track hooks into one of those steps: URL patterns, views, templates that build the HTML, models that load the data.

Settings for development and production

The generated settings.py is set up for development, and three settings must change before a site goes public:

  • DEBUG = True shows detailed error pages with code and settings. In production it must be False, or anyone who triggers an error can read your configuration.
  • SECRET_KEY signs sessions, password reset links and CSRF tokens. The generated one is marked “insecure” and lives in the code; in production, load a long random value from an environment variable.
  • ALLOWED_HOSTS lists the domain names the site answers to, such as ["devtoolbox.example"]. It is ignored while DEBUG is on, which is why requests to localhost work out of the box.

python manage.py check --deploy lists these and other production problems. A common pattern reads values from the environment: DEBUG = os.environ.get("DJANGO_DEBUG") == "1" and SECRET_KEY = os.environ["DJANGO_SECRET_KEY"], so the same code runs safely everywhere.

Practice

Add a second view, about, at /about/ with the name about, returning a short paragraph. Then mount the whole catalog app under /catalog/ instead of the root and check which URLs still work.

Answer check: add path("about/", views.about, name="about") to the app’s list. After changing the include to path("catalog/", include("catalog.urls")), the pages live at /catalog/ and /catalog/about/, and /about/ returns 404. Nothing inside the app changed, which is why apps refer to their URLs by name rather than by path.

Summary

A Django project holds configuration; apps hold features and must be in INSTALLED_APPS. Views take a request and return a response, URL patterns map paths to views, and include lets each app keep its own URL list. migrate creates tables, runserver is for development only, and DEBUG, SECRET_KEY and ALLOWED_HOSTS must be set properly before going live.

All Django chapters