DEV Community

yvonne20865
yvonne20865

Posted on

MVT ARCHICTURE:Models, Views,Templates,URLs.

Django is a powerful, high level web framework built with Python. It’s designed to help developers build clean, efficient, and scalable web apps fast.

At the heart of Django is something called the MVT architecture, which stands for Model, View, Template. It’s pretty similar to the well-known MVC (Model-View-Controller) pattern, just with some tweaks in terminology that make sense in Django’s world.
What is MVT in Django?
Django uses the MVT structure to keep your code organized and maintainable. Let’s break down what that means:
Models — Your Database Tables
In Django, a Model is simply a Python class that defines the structure of your database table. It handles all the data creating, reading, updating, and deleting.

Example:

from django.db import models

class Users(models.Model):
    full_name = models.CharField(max_length=200)
    bio = models.TextField()

    def __str__(self):
        return self.full_name
Enter fullscreen mode Exit fullscreen mode

When you define a model like Users, Django creates a corresponding database table behind the scenes. It’s clean, simple, and powerful.
Views — Your Logic Layer
Views in Django are Python functions or classes that handle incoming requests and return responses — like HTML pages, JSON, redirects, etc.
They interact with your models to get data and pass it to templates for display.
Example:

from django.shortcuts import render
from .models import Users

def user_list(request):
    users = Users.objects.all()
    return render(request, 'users/users_list.html', {'users': users})
Enter fullscreen mode Exit fullscreen mode

This view fetches all users from the database and sends them to a template for display.
Templates — The User Interface
A Template is basically your HTML file. It contains placeholders where dynamic data from your views shows up. Django comes with its own templating language, but you can also use alternatives like Jinja2.

Example Template:

<!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>
Enter fullscreen mode Exit fullscreen mode

Simple, clean, and easy to work with.
URLs — Connecting the Pieces
To make your views accessible via the browser, you define URL patterns like this:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.user_list, name='user_list'),
]
Enter fullscreen mode Exit fullscreen mode

Now when someone visits your site, they’ll hit your view, which talks to your model and renders the template.

Quick Recap
The MVT structure keeps things tidy:
Model: Handles your data
View: Contains your logic
Template: Displays the output
This separation makes your code cleaner and your project easier to scale as it grows.

Hope this clears up how Django apps are structured!
If you have questions or want me to explain anything deeper, drop a comment below.

Top comments (0)