← Back to Django, page 1

django-0001 · Easy fresher

What is Django, and how does its Model-Template-View (MTV) pattern work?

Django is a Python web framework that ships with an ORM, URL routing, templates, forms, an admin site and authentication. It follows MTV: the Model holds data and rules, the View is a Python function or class that handles a request, and the Template turns data into HTML. The URL configuration decides which view runs for each URL.

Full answer

Django is a full-stack Python web framework. "Full-stack" here means it gives you most parts of a web app in one package: a database layer (the ORM), URL routing, an HTML template engine, form handling, a ready-made admin panel, user login, and security protections such as CSRF and SQL injection defence. Current releases are Django 5.2 (a long-term support release) and newer 6.x versions.

Django calls its pattern MTV, which maps closely to the classic MVC idea:

  • Model: a Python class that describes a database table and the rules for its data. A Student model might have name and marks fields.
  • Template: a text file (usually HTML) with placeholders such as {{ student.name }}.
  • View: a Python function or class that receives an HttpRequest and returns an HttpResponse. It reads models and fills a template.

What MVC calls the "controller" is split in Django between the view and the URL configuration (urls.py), which maps a URL pattern to a view.

The flow for one request looks like this:

  1. The browser asks for /students/7/.
  2. Django runs the middleware chain (security, sessions, auth).
  3. The URL resolver finds the matching pattern and calls the view with pk=7.
  4. The view loads Student 7 through the ORM and renders a template.
  5. The response goes back out through middleware to the browser.

Django suits content sites, admin-heavy business apps and APIs (with Django REST Framework). Its strength is that common needs already have a tested, documented answer, so a team writes less glue code.

python
import sys
import types

import django
from django.conf import settings

# Register a tiny in-memory "school" app so this example runs as one file
sys.modules["school"] = types.ModuleType("school")
sys.modules["school"].__file__ = __file__

settings.configure(
    SECRET_KEY="demo-only-key-not-for-production",
    ALLOWED_HOSTS=["testserver"],  # the host name used by Django's test client
    INSTALLED_APPS=["django.contrib.contenttypes", "django.contrib.auth", "school"],
    DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
)
django.setup()

from django.db import models, connection
from django.template import Context, Engine
from django.http import HttpResponse


# Model: data and rules
class Student(models.Model):
    name = models.CharField(max_length=50)
    marks = models.IntegerField()

    class Meta:
        app_label = "school"


with connection.schema_editor() as editor:
    editor.create_model(Student)

Student.objects.create(name="Asha", marks=91)

# Template: presentation
template = Engine().from_string("<p>{{ s.name }} scored {{ s.marks }}</p>")


# View: takes a request, returns a response
def student_detail(request, pk):
    student = Student.objects.get(pk=pk)
    return HttpResponse(template.render(Context({"s": student})))


response = student_detail(request=None, pk=1)
print(response.status_code)
print(response.content.decode())

Standalone script. The first lines register a tiny in-memory app called school and an in-memory SQLite database so the file runs with `python file.py`. In a real project the settings live in settings.py and models live in an app's models.py. Output (ran twice with identical results; Python 3.13.5, Django 5.2.20, djangorestframework 3.18.3): 200 <p>Asha scored 91</p>

Key points

  • Django is a batteries-included Python web framework
  • Model = data and rules, Template = presentation, View = request handling logic
  • urls.py plays the controller role together with views
  • Every request passes through middleware, the URL resolver, a view and back
  • Django 5.2 is the current LTS line

Common mistakes

  • Calling Django an MVC framework without explaining where the controller logic lives
  • Putting business rules in templates instead of models or services
  • Thinking the template is the 'view' in the MVC sense
  • Forgetting that middleware runs before and after every view

Follow-ups

  • Where would you put logic that is shared by several views?
  • What happens if two URL patterns match the same path?
  • When would you choose FastAPI or Flask over Django?

Tips

  • Walk through one request from URL to response; it shows you know the moving parts
  • Name two built-in features (admin, ORM, auth) and why they save time
  • Mention that Django REST Framework is the common choice for APIs on top of Django