Updated May 4, 2023
Introduction to Django Architecture
Django, a free and open-source web application framework written in Python, offers an alternative to servlets, PHP, and JavaScript for developing the backend of web applications. The Django Software Foundation released the initial version of Django on July 15, 2005. The recent version, 2.2.7 of the Django framework, was released on November 4, 2019. Now we will learn about Django architecture with MVT.
Django’s main advantages are making the creation of complicated databases, including web applications, as easy as possible, fast, many components are available implicitly, scalability, and good security. Now, getting into the architecture of Django, it follows MVT.
Django Architecture
As mentioned, Django follows the MVT framework for architecture.
- M stands for Model
- V stands for View
- T stands for template
MVT is generally very similar to MVC, a Model, View, and Controller. The difference between MVC and MVT here is that Django itself does the work done by the controller part in the MVC architecture. Django does this work of controller by using templates. To be precise, the template file combines HTML and the Django Template Language (DTL).
Below is an architecture diagram for MVT.
The template handles the UI and architecture part of an application. The View does the logical part of the application, interacts with the Model to get the data, and modifies the template accordingly. As mentioned earlier, Django functions as a controller that receives a URL connected to the application’s view component and manages user responses within the application. The Django MVT (Model-View-Template) architecture manages the entire process. When we create a project, some default files are created.
I used the above command to create a new project in my my_projects folder. After executing the command, I will show you the files created in the empty folder. These files belong to the first project we created.
There is, in turn, one more folder and a manage.py file that has been created. Now going into the first folder, we can observe the below files.
When creating a new project, the system automatically generates the above files.
- urls.py: Our web page has many links, so all the mappings from one page to others can be done here.
- wsgi.py: We use this to deploy our project.
- manage.py: This gives us a URL where the project can be displayed.
After running this command, the output is as follows.
Output:
Check the output by using the URL in any web browser. We have learned about this, as there should be some start in learning how to create a project on your own and work on it to clearly understand how the MVT architecture runs using Django with Python.
Django Architecture Model
- In Django, the Model is responsible for establishing the connection with the database, where each Model corresponds to a single table. The declaration of fields and methods associated with the Model occurs within the models.py file.
- With this linking to the database, we can actually use every record or row from that particular table and can perform the DML operations on the table.
- Django.db.models.The Model is the subclass that is used here. We can use the import statement by defining it as from django.db import models.
- So after defining our database tables, columns, and records, we need to define the mapping in the settings.py file located under INSTALLED_APPS to link the data to our application.
Django View
- This is the part where we would be mentioning our logic. To execute the coding, the views.py Python file is used.
- The view.py can utilize HttpResponse to send responses while using the application. In other words, it helps in understanding the user’s actions.
- Now, after creating a view, how can we link it to our application? How do you think the system will understand to display a particular view? You can achieve this by mapping the views.py file within the urls.py file. As already mentioned, urls.py keeps track of all the different pages we created and maps each of them.
Django Template
- This template helps us to create a dynamic website easily. The dynamic website deals with dynamic data. Dynamic data handling involves displaying personalized data to users such as Facebook and Instagram feeds.
- To configure the template, go to the INSTALLED_APPS section in the settings.py file. So python code would search for the files under the template subdirectory. We can create an HTML file or import any dynamic web page from the browser and place it under the template folder.
- And after that, our usual linking of this file in urls.py and views.py to get a response is mandatory.
- After linking all these together and running the server, we can get our web application ready.
Django Template Language
In short, it is called DTL. The Django template has syntax for rendering the data onto the web page. For displaying a dynamic variable, the variable name is written inside the curly braces, denoted by “{{variable_name}}”. And to note if conditions, the syntax would be defined as: {% if condition %}. The same would be followed for the end if syntax also. Django uses render function in DTL. This render function takes three parameters.
- Request
- Mentioning the path of the template in settings.py
- Parameters that contain all variables and can create as many as possible.
We can have our DTL make dynamic web pages with these render functions.
Conclusion
So, this is how the it works. The main Python files used to link with each other and display our web page are settings.py, view.py, and urls.py
Once we define our logic here respectively, we can have a workflow linking the database, taking user inputs and responses, and displaying dynamic web pages. After installing Django, please go through the files and understand the linking between them.
Recommended Articles
We hope that this EDUCBA information on “Django Architecture” was beneficial to you. You can view EDUCBA’s recommended articles for more information.