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.