Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

For any piece of code, ask one question: is it about data, decisions, or display? Data → model. Decisions → view. Display → template.

A request for /recipes/Step 1 of 4
  1. Route

    urls.py matches recipes/ and hands the request to the recipe_list view.

The trap is leaking roles: queries inside templates, or HTML strings built inside views. When the designer and the data change for different reasons, they should live in different files.

Drills

Answer fast — trust the rule.

quick drill

In Django's naming, what plays the role classic MVC calls the "view"?

quick drill

Where does title = models.CharField(max_length=200) belong?

quick drill

A designer restyles the recipe cards. Which piece should they need to edit?

quick drill

"If the visitor is not logged in, send them to the login page." Where does this go?

Remember

  • Model holds data, view decides, template shows.
  • Ask "data, decisions or display?" to place any piece of code.
  • Trap: a Django "view" is the controller-like part, not the HTML.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainData, decisions, display

The cookbook needs a page listing quick recipes (under 30 minutes), newest first, each shown as a card. Explain which part of this belongs in the model, which in the view and which in the template, and how Django's names map onto classic MVC.