python #learning #frontend #development
Today I focused on understanding and applying Django’s MVT(Model View Template) architecture, (model, views and templates) the foundation of how Django apps work.
## MODEL – Data layer.
So what is a Model in Django?
I understood Model as a Python class used to define the structure of your database.
Django uses it to create tables automatically, based on your field definitions.
It handles all data-related operations: Create, Read, Update, Delete (CRUD). For example in my previous project I added this kind of model.
from django.db import models
class Item(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
## VIEW – Logic Layer
What is a View in Django?
A View is a Python function (or class) that handles the logic behind a page.
It receives a request, fetches data (usually from the Model), and returns a response (usually a rendered Template).
I created a view to fetch data from the database and send them to the template:
from django.shortcuts import render
def home(request):
return render(request, 'app1/home.html')
## TEMPLATE– Presentation Layer
You are probably wondering what is a Template in Django?
- A Template is an HTML file used to display content to the user.
It uses Django’s built-in Template Language (DTL) to display variables, loop through data, and include logic.
So in both apps I used the the templates as in the previous article.
<!-- blog/templates/blog/post_list.html -->
<!DOCTYPE html>
<html>
<head>
<title>My Users</title>
</head>
<body>
<h1>All Users</h1>
{% for user in users %}
<div>
<h2>{{ user.full_name }}</h2>
<p>{{ user.bio }}</p>
</div>
{% empty %}
<p>No USers available.</p>
{% endfor %}
</body>
</html>
## URL – Routing Layer
what are URLs in Django?
The URL configuration connects user requests (like http://localhost/products) to the correct view.
URLs act like a bridge between the browser and the backend logic.
Then I used my URLs in my previous article.
URL configuration
from django.urls import path
from . import views
urlpatterns = [
path('', views.users_list, name='users_list'),
]
HOW IT ALL CONNECTS (MVT FLOW):
User types http://localhost:8000/products/ URL routes it to views.
- View calls the Product model View
- Model fetches data from the database Model
- View passes data to the template View
- Template displays data in HTML Template
- Summary of Each Part
- Part Django File Purpose
View- views.py Contains logic to fetch/process data
Template templates/.html Displays data using HTML & DTL
URL- urls.py Connects browser requests to views.
Conclusion
Using the MVT architecture separates concerns cleanly:
• Model:
Defines your data.
Defines structure of database tables
• View:
Handles logic and retrieves data.
Contains logic to fetch/process data
• Template:
Renders the output.
Displays data using HTML & DTL
- URLS Connects browser requests to views.
This structure helps you write clean, maintainable code and scale your project efficiently.
My Reflection on Day 5
Understanding how each MVT part plays its role helped me finally see Django as a full web framework — not just scattered files. Now I can create real, working web pages powered by Python and databases, not just static HTML.
Top comments (0)