Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.

At its core, Django follows the MVT architecture: Model, View, and Template — which is similar in principle to the traditional MVC (Model-View-Controller) pattern.

Side note, you can build your Django application using your desired architecture. Django doesn’t necessarily specify any particular form of architectural layering.

This article is an extension of the previous post: https://me.chrisdevcode.com/posts/django-project-structure/

Models

In Django, a model is what defines your database table. It’s basically a python class that maps to a single table in your database. For example class Users will simply be the Users table:

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

    def __str__(self):
        return self.full_name

The above class will create a Users table in your database

A model will contain all the essential fields and behaviors of the data you’re storing.

A Model defines the structure of your database tables. It’s a Python class that maps to a single table in your database.

You can learn more on Models on Django’s documentation:

https://docs.djangoproject.com/en/5.2/topics/db/models/

Views

A View is a Python function or class that takes a request and returns a response. Views fetch data from the model and pass it to the template (the user interface on the browser), or any type of response (a redirect, JSON).

The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as long as it’s on your Python path. There’s no other requirement–no “magic,” so to speak. For the sake of putting the code somewhere, the convention is to put views in a file called views.py, placed in your project or application directory.

A sample view, to get get all Users from the database and displaying on a HTML page:

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

You can learn more about views here:

https://docs.djangoproject.com/en/5.2/topics/http/views/

Templates

A Template is an HTML file that contains placeholders for dynamic data. Django uses its own templating language.

In a Django project, one can use multiple, or even no template engines, depending on whether templates are needed. Django includes built-in support for its own system, the Django Template Language (DTL), as well as for Jinja2, a widely-used alternative.

A template example that displays all users:

<!-- 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 Configuration

Connecting the view to a URL path so users can access it.

from django.urls import path
from . import views

urlpatterns = [
    path('', views.users_list, name='users_list'),
]

Conclusion

Using the MVT architecture separates concerns cleanly: • Model: Defines your data. • View: Handles logic and retrieves data. • Template: Renders the output.

This structure helps you write clean, maintainable code and scale your project efficiently.

Extra Resources - Written

  1. https://forum.djangoproject.com/t/mvc-or-mvt-architecture/3496
  2. https://docs.djangoproject.com/en/5.2/ref/views/
  3. https://docs.djangoproject.com/en/5.2/topics/http/views/
  4. https://docs.djangoproject.com/en/5.2/topics/class-based-views/
  5. https://docs.djangoproject.com/en/5.2/topics/http/generic-views/
  6. https://docs.djangoproject.com/en/5.2/topics/db/models/
  7. https://docs.djangoproject.com/en/5.2/topics/http/urls/