Menu

How Django Works

How Django Works

URL Routing Process

URL routing is how Django decides which view to call for a given request. Every Django project has a root urls.py file, and apps can have their own urls.py files that get included from the root.

The <int:pk> part is a path converter — it captures whatever integer appears at that position in the URL and passes it to the view as a keyword argument named pk. Django has built-in converters for integers, strings, slugs, UUIDs, and paths.

URL names (the name= argument) are important. They let you refer to URLs by name throughout your code instead of hardcoding paths. If you ever change a URL, you only change it in one place.

Views and Templates Flow

A view is just a Python function (or class) that takes an HTTP request and returns an HTTP response. Here's a simple one:

The corresponding template receives the courses variable and uses Django's template language to display it:

The {% %} tags contain logic (loops, conditions). The {{ }} tags output values. This clear separation keeps your Python logic out of your HTML and your HTML out of your Python.

Database Interaction with Models

Models are Python classes that define your data structure. Django reads those class definitions and creates the corresponding database tables automatically.

Once you define the model, you run two commands to create the table:

python manage.py makemigrations

python manage.py migrate

After that, you can interact with the database entirely through Python:

No SQL required.