Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Django MVT Architecture

Refer here
Refer here
Refer here
Refer here

In this article, we will talk about the Django MVT architecture and how is it different from the long-existing MVC architecture. So let’s get right into the topic and understand the Django MVT arhitecture.

But before that, we’ll briefly go over the MVC Architecture here.

What is MVC Architecture?
MVC architecture has been there for a long time in the software industry. All most all languages employ MVC with slight variation, but the concept remains the same.

MVC stands for Model – Views – Controller

Model – A model provides the interface for the data stored in the Database. It is responsible for maintaining the data and handling the logical data structure for the entire web application.
Views – A view in MVC is a user interface. It is responsible for displaying Model Data to the user and also to take up information from the user. Views in MVC is not the same as the Views in Django. We will learn the difference later in this article.
Controller – A controller in MVC is responsible for the entire logic behind the web application. That is when the user uses a view and raises an Http request, the controller sees the user request and sends back the appropriate response.

Image description

Django prefers to use its own logic implementation in its web app and hence its framework handles all the controller parts by itself.

Hence Django implements a different kind of architecture called MVT (Model – View – Template) architecture.

What is the Django MVT architecture?
MVT architecture is the software design pattern used by the Django web framework.

MVT stands for Model – View – Template.

1) Model
Just like the Model in MVC, here as well it has the same functionality of providing the interface for the data stored in the database.

2) Template
Just like View in MVC, Django uses templates in its framework. Templates are responsible for the entire User Interface completely. It handles all the static parts of the webpage along with the HTML, which the users visiting the webpage will perceive.

Image description

Image description

3) Views
In Django, Views act as a link between the Model data and the Templates.

Image description

Note: Just like the controller in MVC, views in Django MVT are responsible for handling all the business logic behind the web app. It acts as a bridge between Models and Templates

It sees the user request, retrieves appropriate data from the database, then renders back the template along with retrieved data.

Therefore there is no separate controller in Django MVT architecture and everything is based on Model -View – Template itself and hence the name MVT.

Control Flow of MVT

Image description

Image description

Here as shown in the above picture

  1. The user sends a URL request for a resource to Django.
  2. Django framework then searches for the URL resource.
  3. If the URL path links up to a View, then that particular View is called.
  4. The View will then interact with the Model and retrieve the appropriate data from the database.
  5. The View then renders back an appropriate template along with the retrieved data to the user.

Project Structure :
A Django Project when initialized contains basic files by default such as manage.py, view.py, etc. A simple project structure is enough to create a single-page application. Here are the major files and their explanations. Inside the geeks_site folder ( project folder ) there will be the following files-

Image description

manage.py- This file is used to interact with your project via the command line(start the server, sync the database… etc). For getting the full list of commands that can be executed by manage.py type this code in the command window-

$ python manage.py help
Enter fullscreen mode Exit fullscreen mode

folder ( geeks_site ) – This folder contains all the packages of your project. Initially, it contains four files –

Image description

init.py – It is a python package. It is invoked when the package or a module in the package is imported. We usually use this to execute package initialization code, for example for the initialization of package-level data.
settings.py – As the name indicates it contains all the website settings. In this file, we register any applications we create, the location of our static files, database configuration details, etc.
urls.py – In this file, we store all links of the project and functions to call.
wsgi.py – This file is used in deploying the project in WSGI. It is used to help your Django application communicate with the webserver.

Image description

Image description

Image description

Top comments (0)