The MVT Pattern
Django organises code using the Model–View–Template (MVT) pattern. Each part has one clear job: models handle data, views handle logic and templates handle presentation. Keeping them separate makes your code easier to read, test and change.
The three parts
| Part | Job | Lives in | Campus Events example |
|---|---|---|---|
| Model | Defines the data and talks to the database | models.py | An Event with a title, venue and start time |
| View | Receives a request, gathers data, decides what to return | views.py | "Fetch all upcoming events and show the list page" |
| Template | Turns data into HTML for the browser | templates/ | An HTML page that loops over the events |
A fourth piece glues them together: the URLconf (urls.py), which maps each web address to a view.
Following one request
Suppose a student visits /events/2/ to see the details of the campus hackathon. Here's what happens, step by step:
- Request arrives. Django wraps the incoming HTTP request in an
HttpRequestobject. - URL matching. Django checks the URL patterns from top to bottom and finds
events/<int:pk>/. It capturespk=2. - View runs. Django calls
event_detail(request, pk=2). - Model query. The view asks for the event with primary key 2. The ORM turns this into SQL, runs it and returns an
Eventobject. - Template renders. The view passes the event to
event_detail.html, which fills in the placeholders. - Response returns. The view returns an
HttpResponsecontaining the HTML, and Django sends it to the browser.
The code for that request
Here are all four pieces side by side. Each one is short because it only does its own job.
events/models.py (the Model)
from django.db import models
class Event(models.Model):
title = models.CharField(max_length=200)
venue = models.CharField(max_length=100)
starts_at = models.DateTimeField()
def __str__(self):
return self.title
events/urls.py (the URLconf)
from django.urls import path
from . import views
app_name = "events"
urlpatterns = [
path("<int:pk>/", views.event_detail, name="event_detail"),
]
events/views.py (the View)
from django.shortcuts import get_object_or_404, render
from .models import Event
def event_detail(request, pk):
event = get_object_or_404(Event, pk=pk)
return render(request, "events/event_detail.html", {"event": event})
events/templates/events/event_detail.html (the Template)
<h1>{{ event.title }}</h1>
<p>Venue: {{ event.venue }}</p>
<p>Starts: {{ event.starts_at|date:"D j M Y, H:i" }}</p>
If event 2 is titled "Campus Hackathon", takes place in ICT Lab 2 and starts at 9:00 on 28 November 2026, the browser receives:
MVT vs. MVC
If you've heard of the Model–View–Controller (MVC) pattern used by frameworks like Laravel or Ruby on Rails, the names can be confusing:
| MVC term | Django equivalent |
|---|---|
| Model | Model |
| View (what the user sees) | Template |
| Controller (the logic) | View, plus Django's URL dispatcher |
In Django, a "view" decides which data to show; the template decides how it looks. The ideas are the same as MVC; only the labels differ.
Why the separation matters
- Teamwork: a designer can edit templates without touching Python, and a back-end developer can change queries without breaking the HTML.
- Reuse: the same model can feed a web page, the admin site and a JSON API.
- Testing: you can test a view's logic without checking every HTML tag.
Putting logic in the wrong place, such as building HTML strings inside a view, or calculating ticket totals inside a template. A good rule: data rules belong in the model, request handling in the view, and display in the template. If a template needs complex logic, move it into a model method or the view.
Key points
- Models describe and store data, views handle requests and templates produce HTML.
- The URLconf connects each address to a view.
- Django's "view" is roughly MVC's "controller", and Django's "template" is MVC's "view".
- Keeping each part to its own job makes projects easier to grow and maintain.
Tip: use the ← and → keys to move between pages.
