Django Basics for a Beginner
Tags: Django, Python Web Developer, Website Design, Python, Web Development

Django Architecture

Django follows the MVT or Model View Template architecture which is based on the MVC or Model View Controller architecture. The main difference between these two is that Django itself takes care of the controller part.

MVC stands for Model View Controller. It is used for developing the web applications, where we break the code into various segments. Here we have 3 segments — model, view and a controller.

Model — Model is used for storing and maintaining your data. It is the backend where your database is defined.

Views — In Django templates, views are in html. View is all about the presentation and it is not at all aware of the backend. Whatever the user is seeing, it is referred to a view.

Controller — Controller is a business logic which will interact with the model and the view.

Now that we have understood MVC, lets learn about Django MVT pattern.

MVT stands for Model View Template. In MVT, there is a predefined template for user interface. Let’s take an example, say you want to write several static html forms like hello user1, hello user2 and so on. With template, you will be having only one file that prints hello along with the variable name. Now this variable will be substituted in that particular template using some jinja logic. That’s the magic of template, you don’t need to rewrite the code again n again!

Now you might be wondering where is the controller?

In the case of MVT, Django itself takes care of the controller part, it’s inbuilt.

That is, it’s probably the framework itself: the machinery that sends a request to the appropriate view, according to the Django URL configuration.

So, in the case of Django, this is what each component is doing:

The MODEL helps to handle database. It is a data access layer, which contains the required fields and behaviors of the data you’re storing. There’s hardly an application without a database. A model is a Python class, and it does not know anything about other Django layers. Models help developers to create, read, update, and delete objects (CRUD operations) in the original database. Also, they hold business logic, custom methods, properties, and other things related to data manipulation.

helps to handle database. It is a data access layer, which contains the required fields and behaviors of the data you’re storing. There’s hardly an application without a database. A model is a Python class, and it does not know anything about other Django layers. Models help developers to create, read, update, and delete objects (CRUD operations) in the original database. Also, they hold business logic, custom methods, properties, and other things related to data manipulation. The VIEW is used to execute the business logic and interact with a model to carry data and renders a template. The view fetches data from a model. Then, it either gives each template access to specific data to be displayed, or it processes data beforehand. It accepts HTTP requests, applies business logic provided by Python classes and methods, and provides HTTP responses to the client requests.

is used to execute the business logic and interact with a model to carry data and renders a template. The view fetches data from a model. Then, it either gives each template access to specific data to be displayed, or it processes data beforehand. It accepts HTTP requests, applies business logic provided by Python classes and methods, and provides HTTP responses to the client requests. The TEMPLATE is a presentation layer that handles the User Interface part completely. These are files with HTML code, which is used to render data. The contents of these files can be static or dynamic. A template is used only to present data since there’s no business logic in it.

A “view” is the Python callback function for a particular URL, because that callback function describes which data is presented. Furthermore, it’s sensible to separate content from presentation — which is where templates come in. In Django, a “view” describes which data is presented, but a view normally delegates to a template, which describes how the data is presented. The ‘controller’ here is Django itself which sends the request to the appropriate view in accordance with the specified URL. This is why Django is referred to as MTV rather than MVC architecture.

The developer provides the model, the view, and the template, then maps it to a URL, and Django does the magic, to serve it to the user.

In short, a web application has data, layout, and logic. The model will work with the data, the view will work with the logic, and the template will work with the layout.