MVC
Most of the web framework implements the MVC(Model-View-Controller) Architecture.
Here Controller intercepts the request and coordinates with the Model and View and sends appropriate response to the user.
The responsibility of the model data definitions, processing logic and interacting with the Database and fetch data or update it accordingly.
The view is the presentation layer of the application. It takes care of the placement and formatting of the result and sends it to the controller, which in turn redirects it to the client as the application’s response.
MVT
Django adapts a Model, View and Template architecture. Here too, the Model is the data layer of the application. The view is, in fact the layer that undertakes the processing logic. (in MVC it was the responsibility of the Controller). And the Template is the presentation layer.
A Django app has the following components:
- URL Dispatcher
- View
- Model
- Template
URL Dispatcher
Django’s URL dispatcher mechanism is equivalent to the controller in the MVC architecture.
The urls.py
module in the Django project’s package folder acts as the dispatcher.
It defines the URL patterns. Each URL pattern is mapped with a view function to be invoked when the client’s request URL is found to be matching with it.
The URL patterns defined in each app under the project are also included. Here’s the urls.py
file in the app folder.
View
The view function reads the path, query, and body parameters included in the client’s request If required, it uses this data to interact with the models to perform CRUD operations.
A view can be a user-defined function or a class.
You create View definitions in the views.py
file of the respective app package folder.Â
The following code in the view.py
file defines the index() view function.
Model
A model is a Python class. Â
An app may have one or more model classes, conventionally put in the models.py
 file.Â
Django migrates the attributes of the model class to construct a database table of a matching structure.
Django’s Object Relational Mapper helps perform CRUD operations in an object-oriented way instead of invoking SQL queries.
The view uses the client’s and the model’s data and renders its response using a template.
TemplateÂ
A template is a web page containing a mix of static HTML and Django Template Language code blocks.
You place Template web pages in the templates folder with the .html extension.
Django’s template processor uses any context data from the view inserted in these blocks to formulate a dynamic response.
The view, in turn, returns the response to the user.
This explains how Django’s MVT architecture handles the request-response cycle in a web application.