Django Architecture
- Understand Django’s MVT architecture and how it manages web application flow.
Why Django Architecture is Important?
Django architecture explains:
How data flows
How pages are generated
How backend & frontend connect
Without understanding MVT, students cannot build real Django projects.
1. What is MVT Architecture?
Django follows MVT Architecture:
MVT = Model – View – Template
It separates:
Data (Model)
Logic (View)
Presentation (Template)
This makes applications:
Clean
Scalable
Easy to maintain
MVT vs MVC (For Understanding)
👉 In Django, View acts like Controller
Code Example: MVT Folder Structure
Code Title: Django MVT Structure
Code Description: Shows how Django organizes MVT components
myapp/
│
├── models.py # Model
├── views.py # View
├── templates/
│ └── home.html # Template
├── urls.py
2. Model (M in MVT)
What is Model?
A Model:
Represents database table
Handles data logic
Written as Python class
👉 One model = One table
Responsibilities of Model:
Define database schema
Store & retrieve data
Business logic
Code Example: Django Model
Student Model
Creates a database table using Python class
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
age = models.IntegerField()
def __str__(self):
return self.name
Explanation:
Student → Table name
Fields → Columns
Django ORM handles SQL
3. View (V in MVT)
What is View?
A View:
Handles business logic
Receives HTTP request
Returns HTTP response
👉 View decides what data to send and which template to use
Responsibilities of View:
Handle request
Communicate with Model
Send data to Template
Code Example: Django View
Student View
Fetches data from model and sends to template
from django.shortcuts import render
from .models import Student
def student_list(request):
students = Student.objects.all()
return render(request, "students.html", {"students": students})
Explanation:
Fetches data using ORM
Sends data to template
render() combines view + template
4. Template (T in MVT)
What is Template?
A Template:
Handles UI (HTML)
Displays dynamic data
Uses Django Template Language (DTL)
👉 No Python logic inside templates
Responsibilities of Template:
Display data
Loop & condition rendering
UI presentation
Code Example: Django Template
Student Template
Displays dynamic data using DTL
<!DOCTYPE html>
<html>
<head>
<title>Students</title>
</head>
<body>
<h1>Student List</h1>
{% for student in students %}
<p>{{ student.name }} - {{ student.email }}</p>
{% endfor %}
</body>
</html>
Explanation:
{{ }} → Variable
{% %} → Logic
Clean separation of UI
5. Request–Response Cycle
What is Request–Response Cycle?
It explains how a user request becomes a web page.
Step-by-Step Flow:
1. User opens URL
2. Request goes to urls.py
3. URL maps to View
4. View interacts with Model
5. View sends data to Template
6. Template generates HTML
7. Response sent back to browserVisual Flow:
Browser → URL → View → Model
↓
Template
↓
Response
Code Example: URL → View Mapping
Django URL Configuration
Connects URL with View
from django.urls import path
from .views import student_list
urlpatterns = [
path("students/", student_list, name="students"),
]
Explanation:
URL triggers view
View returns response
6. Complete MVT Flow Example
Example:
User opens /students/
Model
class Student(models.Model):
name = models.CharField(max_length=100)
View
def student_list(request):
students = Student.objects.all()
return render(request, "students.html", {"students": students})
Template
{% for student in students %}
<p>{{ student.name }}</p>
{% endfor %}