MVC in Django

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

MODEL VIEW

CONTROLLER (MVC)
Django MVC: MTV
MVC ARCHITECTURE
• Web applications have seen a great evolution from simple HTML pages to complex designs
and frameworks included.
• To handle the complexity and increase code reusability, different patterns were used in the
implementation of these projects which made it easy for developers to work with it and out
of all the patterns MVC became the most popular in use.
• MVC architecture is an organized software architectural design pattern which is used to
divide application functions and program logic into different components.
• Today it's widely used in web application design. This concept could easily be found in
many web development frameworks nowadays such as Angular (JS), Django (Python), etc.
• MVC architecture has three components as the name suggests, Model, View, and Controller.
• Model
• This component handles all the data-logic which means that it is the one that interacts with
the database and handles all the data operations.
• It can not interact directly with the user and it is the only one to interact directly with the
database.
• It responds to the instructions given by the Controller whether it is fetching data or updating
it.
• View
• This component is responsible for displaying the data to the user in a suitable format.
• It is the actual view of the application or what the user sees.
• It interacts with the controller to receive the retrieved data.
• Controller
• It is that component that is responsible for handling user interaction which means it is the
one that receives the input and requests given by the user.
• It handles all the communications between Model and View.
• It sends instructions to the model for updating or retrieval and view for updating the view.
DJANGO MVC: MTV
• Django is a high-level python web framework which is widely used nowadays because of its
clean design and it uses Model Template View design which is similar to the concept of
MVC.
• The Model Template View (MTV) is slightly different from the MVC. The official Django
documentation states that it could use the term Templates for Views in MVC and the term
Views for Controller in MVC and Django itself takes care of the Controller part.
• The flow of a request being processed in Django is as follows.
• First, incoming requests to the Django server will be processed through urls to be forwarded
to the views defined by the developer to process the request.
• If there is a process that requires the involvement of a database, then the views will call a
query to models and the database will return the result of the query to the views.
• After the request has been processed, the result of the process will be mapped into the
HTML that has been defined before finally the HTML is returned to the user as a response.

You might also like