The MVT Architecture
Django organizes application code using the Model-View-Template (MVT) pattern, a variant of the classic Model-View-Controller (MVC) pattern. In MVT, the Model defines the data structure and business rules (typically a Python class in models.py mapped to a database table), the Template defines how data is presented as HTML, and the View contains the logic that fetches data from the Model and passes it to the Template. The key difference from traditional MVC is that Django itself acts as the controller, using its URL dispatcher to route each incoming request to the correct view function automatically, so developers don't write a separate controller layer.
Cricket analogy: Similar to how a cricket match separates the scorer (Model, recording runs and wickets), the scoreboard display (Template, showing the score to the crowd), and the umpire's decision logic (View, deciding what to display next), Django's MVT splits data, presentation, and logic into three distinct roles.
Models: Defining Data
A Model in Django is a Python class that subclasses django.db.models.Model, where each class attribute becomes a database column via a field type such as CharField, IntegerField, or ForeignKey. Django's ORM translates this Python class into SQL automatically: calling Article.objects.filter(published=True) generates a SELECT query with a WHERE clause without the developer writing raw SQL, and running python manage.py makemigrations followed by migrate generates and applies the corresponding CREATE TABLE or ALTER TABLE statements.
Cricket analogy: Similar to how a scorecard defines fixed fields for every player — runs, balls faced, and dismissal type — that get filled in identically for every match, a Django Model defines fixed fields like title and published that get filled in identically for every row in the database.
Views and Templates: Logic and Presentation
A View is a Python function or class in views.py that receives an HttpRequest, fetches or manipulates data through the Model layer, and returns an HttpResponse, usually by rendering a Template. Templates live in .html files using Django's own template language, which supports variable interpolation like {{ article.title }} and control-flow tags like {% for article in articles %}, but deliberately restricts arbitrary Python execution inside templates to keep presentation logic simple and separate from business logic, which belongs in the view or the model.
Cricket analogy: Similar to how a match referee decides what data to relay to the broadcast (View) while the graphics team formats it for the scoreboard (Template), a Django view fetches article data and passes it to a template that renders {{ article.title }} for display.
# models.py
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
published = models.BooleanField(default=False)
# views.py
from django.shortcuts import render
from .models import Article
def article_list(request):
articles = Article.objects.filter(published=True)
return render(request, 'articles/list.html', {'articles': articles})
<!-- templates/articles/list.html -->
{% for article in articles %}
<h2>{{ article.title }}</h2>
{% endfor %}Business logic belongs in the model or view, not the template. Django's template language intentionally cannot run arbitrary Python, so pushing calculations into templates leads to convoluted, hard-to-test template tags.
- MVT stands for Model-View-Template, Django's variant of the MVC pattern.
- The Model defines data structure and lives in
models.py. - The View contains logic that fetches data and returns an
HttpResponse. - The Template renders HTML using Django's restricted template language.
- Django itself acts as the controller via its URL dispatcher.
- Templates deliberately cannot execute arbitrary Python for separation of concerns.
makemigrationsandmigratekeep the database schema in sync with Model changes.
Practice what you learned
1. In Django's MVT pattern, what does the 'T' stand for?
2. Which Django component acts as the 'controller' in traditional MVC terms?
3. Where should business logic and calculations typically live in a Django app?
4. What does a Django View function typically return?
5. What command applies pending schema changes from a Model to the database?
Was this page helpful?
You May Also Like
What Is Django?
An introduction to Django, the high-level Python web framework that ships with an ORM, admin site, and authentication built in, so teams can move from idea to working application quickly.
Creating a Django Project
A practical walkthrough of scaffolding a new Django project with django-admin, understanding the generated files, and running the development server for the first time.
Django Apps Explained
Why Django separates a project into reusable 'apps', how to create one with startapp, and how to register it so Django picks up its models and views.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentNext.js Study Notes
JavaScript · 30 topics