Learn Django Architecture in just 5 minutes

For interacting with any websites out there on the Internet we need 3 things, Input, Processing, and Output. In technical words, Input logic, Business logic, and UI logic.   

Back in times, when our web applications were not very interactive and we as a user did not demand much from it, it was quite effective to have all the 3 components clubbed in one. The early web pages were majorly text-based, with no special design and page structures, the internet connections were slow and technology limited.

But, today, our web applications are very smart. Machine learning algorithms are introduced to understand customer behavior and boost customer engagements, using AI chatbots to improve user experience and all the other magical things out there. We need a proper division of work.

For solving this problem, we have an approach popularly known as MVC. MVC stands for Model, View, and Controller. MVC solves the drawback of the traditional approach of having codes in one file.

The UI logic is an HTML, CSS page made for the users to interact with the system. Business logic is the part that handles the output. The Input logic takes the input and sends it to the Business logic part for processing the user’s request.

Let’s understand the MVC model in more depth.

Model-View-Controller in Django

MVC in Django

Let us discuss this in a way in which a user looks up a web application. The first thing being the user interface, i.e., View, then the Controller, and finally the Model.

1. View

View provides a user interface, it displays the actual view of the web application you are using. It is an interactive end for the users, which provides data stored in the form of charts, tables, diagrams, text boxes, dropdown menus, etc.

It takes the user’s input and informs the Controller. The Controller further passes the request to the Model. Controller and Model tell the view of the things to display the users. It represents the UI logic.

2. Controller

The Controller may be referred to as the most important part of the MVC model. The controller is that part of the MVC model, which accepts user input and performs the corresponding updates both in model and view.  It acts as a bridge between a user and the system, i.e., between View and Model.

It provides the user the option to make an input by displaying the appropriate View. The Controller uses logic to interpret the user’s input and make a request to the Model. The Model understands and grabs the data, the controller then understands the output, converts it back to the appropriate format, and passes the message back to the View for the users to see that. It represents the Input logic.

3. Model

The Model is the lowest level of the MVC pattern which is held responsible for storing and maintaining data. This level tells where the web application’s data is stored. It responds to the instructions from the Controller and also to the requests from the View. It represents Business logic.

MVC Real life Example

1. Let’s assume you go to a coffee shop. You are the customer, you are not going to make the coffee, right?

2. Now the waiter comes to you and asks for the order. He just wrote down what you want, he is also not going to make coffee for you, he doesn’t know how to make it.

3. The waiter gives your order to the cook in the kitchen.

4. The cook then prepares your coffee with all the ingredients which he has on the shelf.

5. Cook finally handed over the coffee to the waiter. Now the waiter brings that coffee to you.

So in this case:

View = You

Controller = Waiter

Model = Cook

Data = Shelf

Django MVT Pattern

Django follows MVT ( Model-View-Template) architecture, wherein Template is replaced for View and View for Controller. The working is the same with just change in the terminologies.

Though, there is a misconception that the view in MVT works the same as the controller in MVC. But it is not actually true. We will discuss this in more depth in the next article on Django’s MVT architecture.

For the time being, this image will help you to know the overview. 

Django Architecture and Components

Advantages of Django Architecture

1. Rapid Development

As there is the correct distribution of work between 3 components, rapid application development is achieved. A programmer can work on views in the MVC model, one on the Controller, and one on the Model. This easily distributes the work among the programmers hence help in developing the applications fast.

2. Loosely Coupled

The Django applications have three different components working one at a time. All the 3 components are designed individually so this has made security very important aspects during the development

3. Asynchronous Techniques

The MVC architecture can easily integrate with the Javascript Framework. This helps the applications to work with PDF files and desktop widgets. 

4. Modification is easy

The modification is necessary for any growing web application. Even the smallest changes of colors, fonts, images are quite easy while working with the MVC architecture. As the 3 components are loosely coupled and don’t depend upon others, the changes made in View have nothing to do with the Model.

5. Returns Data Without Formatting

MVC returns data without applying it to format. That means the same components can be used with any other framework.

6. Seo Friendly

We have already discussed this point in our articles. MVC model helps in creating SEO-friendly web applications.

Disadvantages of using MVC Architecture of Django

1. The Navigation of the framework can be challenging sometimes. The introduction of three layers with abstractions involved makes the users learn the decomposition criteria of MVC.

2. Need multiple developers to perform parallel programming.

3. Knowledge of multiple technologies is necessary.

4. Maintenance of lots of codes in Controller. 

Summary

This tutorial discussed the MVC model (Model-View-Controller), the MVT model (Model-View-Template), and the advantages and disadvantages of the MVC model.