dev-resources.site
for different kinds of informations.
What is the Architecture of Django?
The first time I was asked this question was not when i was learning Django, but after I had learned it and applied for an internship. During the internship, I was asked this question. Unfortunately, I didn't know the answer at that time, but now I do.
Every Django project you create follows an architecture called MVT. The MVT here stands for Mode Template View. These three things are main parts of any Django project. Let's see about them in detail.
Model
A model is a class based representation of a table in a database. Django applications use python classes to represent a table in the website database, these classes are called models in django. All the model classes you create in django should inherit from "django.db.models.Model" class. Each model class will have attributes that represents the fields of the database table.
View
The view is the function or class that contains necessary logic to take in a HTTP request coming from the client and send the appropriate HTML, json or some other response back to the user. In Django these views can be class based or function based in Django.
The view takes in the url path, query parameters along with request body sent by the user/client then uses this data to perform CRUD operations if needed, and sends back the responses.
Template
The template in Django is nothing but a HTML file that defines the layout and contains body of webpage along with some other script written in special templating syntax supported by Django.
Using this special templating syntax we can show the dynamic data on the website. This dynamic data is actually given by a Django view to the template and it usually contains the information about models of the Django project.
MVT and MVC?
The MVT architecture used by Django is a slightly different version of another popular architecture called MVC. Here MVC stands for Model View Controller. Here Model, View and Controller stands for
Model: The model here represents the data and business logic. The model handles the data and business logic of the application similar to the model in Django's MVT.
View: The view here is different. It doesn't contain the logic for handling http requests, instead it represents UI elements. So, the view in MVC is more similar to template in MVT
Controller: The Controller in this architecture is responsible for the logic for controlling the requests and user inputs. So controller here is more like view in the MVC
So, this is all you should know about the Django's architecture. If you have any questions feel free to ask them in comments.
Featured ones: