The MTV design pattern in Django and its relationship to MVC

Before we start programming, let's talk a bit about these pieces of software known as frameworks.

What is a Framework?

A framework is a set of easily reusable, scalable and relatively easy-to-maintain components. The great thing about this is that when we develop an application there are always structures that are repeated throughout the development cycle; for example:

  • Database connection
  • User module (Register, start, session, close session)
  • Administration panel or data management
  • Forms and file upload
  • Among others

And these components can be reused in most applications today, which in general terms are based on CRUDs, removing some operations depending on the module we are working on, for example:

If we have a list of tasks, we perfectly apply a CRUD process to manage it; but if we have a list of Posts that we want to present to the user, that is, a blog for our end user, we only care about the reading process.
Of course, all of these have ways of being seen by our user (View Layer) and managed at the data level (Controller and Model Layer) and here the interesting thing about implementing patterns to make applications modular and scalable over time, and here comes the MVC and its variations such as MTV.

Getting to know the MVC pattern

The first thing we are going to talk about is that it is a pattern; A pattern is simply a set of techniques to solve common problems; and our MVC is a pattern and now we are going to talk about what each of its acronyms mean.

Model View Controller (MVC)

  1. M stands for “Model” (Model), which is the layer that offers us the connection to the database managed as an object (ORM) through the framework; this layer is in charge of communication and interaction with the database.
  2. V means "View" (View), the layer that is responsible for presenting the data to our user (Frontend); and these data are generally those obtained from the database thanks to the previous layer, which are passed to it (to the View) after prior controller processing.
  3. C means "Controller", the layer where we will spend most of our time Backend (business logic), this layer is the intermediary (Middleware) that is responsible for receiving the request from our client (through a URL or route) and do, whatever it is intended to do; which generally consists of getting the data from our database through the model, processing it, and assigning the view and passing the data to it.

MTV vs MVC

Now that we know that it is a framework that in very few words would be a kit or set of tools with reusable and modifiable components; we also talked about what a pattern is and how the MVC pattern works, we're going to talk about how frameworks like Django are composed.

If you come from PHP like I am, you will know that the most popular frameworks such as CodeIgniter and Laravel work with MVC, that is, the Model View Controller although each of them are certain modifications in it.

The ideal of using these patterns when developing our applications is that each layer is independent of the other and we have a separation between each of the layers.

Django and its variation of classic MVC

Django implements this MVC pattern in a peculiar way and with some variations that they call MTV, which is Model, Template, View.

But it's practically one-to-one equivalence with MVC on a change more of interception than anything else; the M, V and C are separated in Django as follows:

  1. M, the M of Model has remained the same, which has been the access layer to the database.
  2. V, the layer that specifies what data we are going to show and defines the presentation of the data to our user, the only difference is that Django views of our MVC are known as Template, therefore we go from V to T.. .
  3. C, the layer that is in charge of connecting the Model with the View, this layer in Django could be said to be managed (or at least in part) by the framework itself; since it is Django that is in charge of handling the routing or the handling of the routes (intercept the request of our client and assign to the "Controller") which specifies which route is going to call which function in our project in Django URLConf, and For this reason, we have other types of layers that simply call it "View".

So in summary:

  1. M stands for "Model" (Model), where it is still, the layer that is responsible for communicating and interacting with the database.
  2. T means “Template” (Template), in this case the template fulfills the function of our view, and it is the one we use to display the data of our user, generally the HTML page.
  3. V stands for “View” (Vista), the “C” in the MTV design pattern, is the layer that is responsible for managing the business logic, through which the data from the model will pass to the template.

To compare with other frameworks such as Laravel or CodeIgniter 4, it also has another "layer" that is responsible for handling the routing of the application, in CodeIgniter 3, this mechanism also exists but it is completely optional and we can relate one to one a controller with a URL.

Thus there is a somewhat problematic name combination between the MVC naming and its MTV variation, in the Django folks' interpretation of "view".
Similar to the Controller, Django's View (the controller in MVC) describes the data that is presented to the user; not necessarily how it will be displayed, but what data is presented.

Here is a link to the official documentation where you can get more details about the framework.

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.