Model View Controller (MVC) and link with Django (MTV)


Reading time: 20 minutes | Question Solving time: 5 minutes

In this article, we'll be learning about the Model View Controller (MVC) architecture and how it is used in web development. We'll also learn about its use specifically in Django (Python).



Now, 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.

Now to define it formally, MVC architecture is an organized software architectural design pattern which is used to divide application functions and program logic into different components. Initially, this architecture was used to design desktop user interfaces (GUIs) but today it's widely used in web application design. This concept could easily be found in many web development frameworks nowadays such as Ruby on Rails (Ruby), Zend (PHP), Express (JS), Angular (JS), Django (Python), etc.

MVC architecture has three components as the name suggests, Model, View, and Controller let's see what these do.

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(GET, POST, etc) 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.

Now, to understand better let's see the actual implementation of MVC and how Model, View, and Controller interacts through a flowchart.

MVC-3

  • Whatever the user sees on the screen is handled by VIEW. It usually consists of HTML and CSS.
  • When the user gives the input or any request the validation is done by the CONTROLLER and it sends further instruction to MODEL.
  • MODEL handles all the data operations and returns the data to the CONTROLLER or in case of an update informs the VIEW about all the changes.
  • VIEW and CONTROLLER together handles the user interaction and requests

*Working of the MVC model could vary a bit in different frameworks.

Now, even though the concept of MVC is used widely in different frameworks, it could be confusing to understand how it works in frameworks such as Django. Let us see what all differences are there in the implementation of Django MVC.

Django MVC: MTV

Now, those who are new to this, 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.

MVC MTV
Model Model
View Template
Controller View
  • The model component and its function remain the same here.
  • The template component handles overall how the data would be presented to the user.
  • The view component here describes what data is presented and it does not matter in which way it is being presented.
  • The controller part which also handles URLs is taken over by the framework itself.

Refer Django documentation

URL

  • Views are implemented in the form of callback functions which returns data for a particular URL.('views.py' file contains all the views)
  • Using views a template is rendered, templates in Django are nothing but HTML files mixed with Django Template Language(DTL). ( A separate 'Templates' folder should be created containing all the HTML files for handling templates)
  • A separate 'models.py' file contains all the Models.
  • MTV is also called 'MTV + Controller' but the Controller word is often removed because that part is already included in the framework.

Summary

Now let's have a quick recap of what we learned:

  • MVC is a design pattern popularly used to create web applications that consist of 3 components namely Model, View, and Controller.
  • The Model manages the data-logic, View manages the user interface and the Controller interacts with the user and controls Model and View.
  • The concept of MVC could be applied differently in different frameworks.
  • MTV is used in Django wherein Views in MVC are implemented in the form of Templates(T) and the Controller part is implemented by the framework and Views(V).

Time to test yourself

Question 1

Consider a blog application, what will be the task of the Model?

Fetching the blog data for View
Sending the blog data to the Controller
Displaying the data to the user
None of the above
The Controller will ask model to fetch the data and the Model will send the data.

Question 2

Consider a blog application, what will be the task of the View?

Passes the blog data to the Model
Updates the data
None of the above
Displays the data to the user
The View will use the blog data recieved by the Controller to create an HTML page generally.

Question 3

Consider a blog application, what will be the task of the Controller?

Passes the blog data to the View
Updates the database
Displays the data to the user
None of the above
Here the Controller will fetch the blog data from the Model then pass the data to the View and return HTML to the client.

With this article at OpenGenus, you must have the complete idea of Model View Controller (MVC) and link with Django (MTV). Enjoy.