← Back to Django Course | Chapter 1: Django Fundamentals & Setup | Lesson 9 of 9

Django MVT Architecture

MVT is Django's way of splitting jobs into three teams: one team stores data, one team decides what to do, and one team shows the page.

What MVT Stands For

MVT stands for Model, View, Template — the three layers Django separates a web application into, each with a single clear responsibility.

Example: What MVT Stands For

One view touches all three MVT layers: Article.objects.all() reads the Model, the function itself is the View logic, and render() hands the data to the Template.

markup
# views.py
from django.shortcuts import render
from .models import Article

def article_list(request):
    articles = Article.objects.all()                          # Model
    return render(request, 'article_list.html', {'articles': articles})  # View + Template
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

The Model Layer

The Model layer defines your data structure and talks to the database; it is where you describe what information your app stores, like a blog post or a user.

Example: The Model Layer

This Model layer code defines exactly what an Article is and how it's stored — Django turns this class into a real database table.

markup
# models.py
from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    published = models.DateTimeField(auto_now_add=True)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

The View Layer

The View layer contains the logic that decides which data to fetch and which template to render in response to a request — it is the "brain" connecting models and templates.

Example: The View Layer

The View layer's job is decision-making: here it decides to fetch only published articles, then names article_list.html as the Template that will display them.

markup
# views.py
from django.shortcuts import render
from .models import Article

def published_articles(request):
    articles = Article.objects.filter(published=True)
    return render(request, 'article_list.html', {'articles': articles})
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

The Template Layer

The Template layer is the HTML file with placeholders that Django fills in with real data, controlling exactly how the page looks to the user.

Example: The Template Layer

This is the Template layer: it never touches the database directly, it only loops over the articles the View already fetched and renders them as HTML.

markup
<!-- article_list.html -->
{% for article in articles %}
  <h2>{{ article.title }}</h2>
  <p>{{ article.body }}</p>
{% endfor %}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

MVT vs Traditional MVC

Django's MVT is similar to MVC, but Django itself acts as the Controller by routing requests to views, so developers only write the Model, View, and Template parts.

Note: Django is sometimes described as "MVC, but Django handles the Controller for you."

Example: MVT vs Traditional MVC

In traditional MVC you write a Controller to route requests; in Django, urls.py plus the framework's own request handling does that job for you — you only write the Model, View, and Template parts.

markup
# urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('articles/', views.article_list),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Confusing Django's MVT pattern with the traditional MVC pattern used by other frameworks.
  2. Putting business logic directly inside templates instead of in views.
  3. Thinking the "view" in Django MVT is what the user sees, when it is actually the controller-like logic layer.
Chapter Summary
  • MVT stands for Model, View, Template.
  • The Model manages data and talks to the database.
  • The View contains the logic that decides what data to send.
  • The Template controls how that data is displayed as HTML.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.