Skip to the workspace
Pebellslearn by building
Introduction to Django  ›  Capstone Project

The MVC Model (Django's MTV)

The MVC Model 14 minDjango splits a site into models that hold data, views that decide what to do, and templates that show the result, which is its version of the model-view-controller pattern.

django / The MVC Model (Django's MTV)

Explore each scene at your own pace.

01 · The rule

The rule

The MVC Model (Django's MTV)

Open any real Django project and the code is spread over files called models.py, views.py and a templates folder. That split is not an accident. It follows an old, reliable idea for keeping a web app understandable as it grows: keep the data, the decisions and the display apart.

The rule

Model holds data, view decides, template shows.

Classic MVC (model–view–controller) names three roles. Django keeps the idea but uses its own names, often called MTV:

Role Classic MVC Django
The data and its rules Model Model (models.py)
Deciding what to do with a request Controller View (views.py), reached through urls.py
What the user sees View Template (.html files)

The naming clash catches everyone once: in Django, a view is the decision-maker, not the display.

Here are all three for the cookbook's recipe list:

# models.py: the data
class Recipe(models.Model):
    title = models.CharField(max_length=200)
    minutes = models.PositiveIntegerField()

# views.py: the decision
def recipe_list(request):
    quick = Recipe.objects.filter(minutes__lt=30)
    return render(request, "recipes/list.html", {"recipes": quick})
{# templates/recipes/list.html: the display #}
{% for recipe in recipes %}
  <article class="card">{{ recipe.title }}, {{ recipe.minutes }} min</article>
{% endfor %}

Django's own routing machinery, the part that receives the request and picks a view, does the rest of the controller's job.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.