Django MTV Architecture – Understanding Components & Working
In our previous tutorial, we learned about the Django architecture on MVC pattern and how modern web-programming is being implemented and also studied some advantages of MVC which were the same as offered by Django (MVC based Framework). Although Django at its core is based on MVC architecture, it actually is implementing a variation of MVC, called MTV architecture.
There are some drawbacks of MVC architecture and it has certain areas where Django is offering a better-quality feature when using the MTV architecture.
We will first learn the components of MTV architecture in detail.
Keeping you updated with latest technology trends, Join DataFlair on Telegram
MTV Architecture Components (Model, Template, and View)
This is a variation of the MVC pattern as you can see in the acronym itself the Template keyword replaces the Controller. Although, the Template is not exactly functioning as the controller and has some different properties than the controller.
The definitions of Model still remain the same that is, the Model contains the logical file structure of the project and is the middleware & data handler between database and view. The Model provides a definition of how the data formats as coming from the view so, it stores in the database and vice-versa, i.e., the retrieving information from the database transfers to the view in the displayable format.
The View in MTV architecture can look like the controller, but it’s not. The View in this MTV architecture is formatting the data via the model. In turn, it communicates to the database and that data which transfer to the template for viewing.
What Template is doing if everything View achieves in the first place?
Well, the template is making the life of a frontend developer easy that’s for sure. It also provides more development speed then the traditional MVC architecture would.
So, how is it doing that?
Template’s main goal is to keep everything that browser renders. The model’s data that’s coming from the server in different parts while integrating the same when the user interacts with the website. Here, the template layer in Django is more similar to the views layer in MVC pattern. This layer is more focused and with Django framework, it provides much more extendibility to the frontend developers than what MVC architecture was giving.
Working of MTV Architecture
Now for a better understanding of how Django implements the process, and what is the really important thing in this MTV architecture. We can examine the below Django architecture diagram to understand it.
As from the above diagram, we have some components and two regions i.e., server side and client side. Here you will notice that the View is on the server-side part while the template is on the client side.
Now, when we request for the website, the interface through which we use to make that request via our browser was the Template. Then that request transmits to the server for the management of view file.
Django is literally a play between the requests and responses. So whenever our Template is updating it’s the input (request) we sent from here which on the server was seen by the View. And, then it transports to the correct URL. It’s one of the important components of Django MTV architecture. There, the URL mapping in Django is actually done in regular expressions. These expressions are much more understandable than IP addresses. It also helps with the SEO task which we have discussed in the Django Features Tutorial.
Now after the sending of request to the correct URL, the app logic applies and the model initiates to correct response to the given request. Then that particular response is sent back to the View where it again examines the response and transmits it as an HTTP response or desired user format. Then, it again renders by the browser via Templates.
An easier real-life working of above functioning would be –
When you login in a website (Django based), you open the login page. It again happens without the need of the Model. It is because Views will process the request and send it to the URL of the login page. Then, it will be a response by the server, from there to the browser.
After that, you enter your credentials in the given Template, HTML form. From there the data is again sent to the view, this time this request rectifies and the model is given data. Then the Model reads and verifies the data that user provides within the connected database.
If the user data matches it will send the relevant user data like profile image, name and (other things depending on the type of website) to the Views. It will then format the same in desired response and will transmit the same to the client.
Otherwise, the Model will send a negative result to the Views. In turn, it will rout it to the login page again alongside an error message.
That’s how the Django MTV architecture is actually working.
Some Drawbacks of MVC Architecture
i. Two components are controlling View
In the MVC pattern, the view is the UI/ display logic of webpage. But in this MTV architecture, it generates/controls by two components, Model and Controller. That makes the testing process for View a bit difficult. As to fully test that, we will actually have to write the required model and controller first.
ii. Too much load on Model Component
Model is doing all the work, be it transferring data from and to the database and operating on it. Also giving the appropriate results via applying the business logic. Single component accomplishes all these tasks.
iii. Development Complexity is high
If we are implementing the MVC architecture’s approach over smaller projects then it would rather be a lengthy process of development. We will always need to understand the project structure and have to be cautious of the conventions that are using.
It has opposite effects on the smaller applications as they may not be too scalable. It can be like some ERP websites for college students, office private server etc. There this approach may make the system slower rather than faster.
These were some of the major drawbacks of MVC architecture. Though some of these drawbacks are resolved by the MTV architecture. But still, you would need to be familiar with the project/ file structure of the Django for using this technology for development.
In this tutorial, we learned that what are the basic components of Django MTV architecture that create the Django project. This tutorial was the very basics of Django and you had to have this knowledge if you are going to use Django even in smaller projects.