Skip to content
Kuza Ndoto Academy

The MVT Pattern

IntermediateChapter 2 · Page 5 of 233 min readUpdated 15 Nov 2026

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

PartJobLives inCampus Events example
ModelDefines the data and talks to the databasemodels.pyAn Event with a title, venue and start time
ViewReceives a request, gathers data, decides what to returnviews.py"Fetch all upcoming events and show the list page"
TemplateTurns data into HTML for the browsertemplates/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:

  1. Request arrives. Django wraps the incoming HTTP request in an HttpRequest object.
  2. URL matching. Django checks the URL patterns from top to bottom and finds events/<int:pk>/. It captures pk=2.
  3. View runs. Django calls event_detail(request, pk=2).
  4. Model query. The view asks for the event with primary key 2. The ORM turns this into SQL, runs it and returns an Event object.
  5. Template renders. The view passes the event to event_detail.html, which fills in the placeholders.
  6. Response returns. The view returns an HttpResponse containing the HTML, and Django sends it to the browser.
Browser 1 urls.py 2 View 3 Model 4 SQL Database 5 render() Template 6 HTML response
Figure 1. The numbers show the order in which things happen. Only the model talks to the database, and only the template produces HTML.

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:

<h1>Campus Hackathon</h1> <p>Venue: ICT Lab 2</p> <p>Starts: Sat 28 Nov 2026, 09:00</p>

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 termDjango equivalent
ModelModel
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.
Common mistake

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.
Up next · URLs & ViewsWriting ViewsFunctions that return web pages.
→

Tip: use the ← and → keys to move between pages.