Introduction to Django
Basic Introduction
Django is an open-source web application framework written in Python.
Using Django, Python developers can easily accomplish most of the content required for a formal website with minimal code and further develop fully functional web services. Django itself is based on the MVC model, which stands for Model + View + Controller design pattern. This MVC pattern simplifies subsequent modifications and extensions to the program and allows for the reuse of certain parts of the program.
MVC Advantages:
- Low coupling
- Quick development
- Easy deployment
- High reusability
- Low maintenance costs
- ...
Python combined with Django is the best combination for rapid development, design, and deployment of websites.
Features
- Powerful database functionality
- Built-in robust admin features
- Elegant URLs
MVC and MTV Models
MVC Model
The MVC pattern (Model-view-controller) is a software architectural pattern in software engineering that divides the software system into three basic parts: Model, View, and Controller.
MVC connects in a plug-in and loosely coupled manner.
- Model (M) - Writes the program's functionality and is responsible for the mapping of business objects to the database (ORM).
- View (V) - Graphical interface, responsible for user interaction (pages).
- Controller (C) - Responsible for forwarding requests and processing requests.
Simplified Diagram:
User Operation Flowchart:
MTV Model
Django's MTV pattern is essentially the same as MVC, aiming to maintain loose coupling between components, with only slight differences in definition. Django's MTV stands for:
- M represents Model: Writes the program's functionality and is responsible for the mapping of business objects to the database (ORM).
- T represents Template: Responsible for how the page (html) is presented to the user.
- V represents View: Responsible for business logic and calling Model and Template when appropriate.
In addition to these three layers, there is also a URL dispatcher, which is responsible for routing page requests for individual URLs to different Views for processing. The View then calls the appropriate Model and Template. The response pattern of MTV is as follows:
Simplified Diagram:
User Operation Flowchart:
Explanation:
When a user makes a request (request) through their browser to our server, this request accesses the view function:
- a. If there is no data call involved, the view function directly returns a template, which is a webpage, to the user.
- b. If there is a data call involved, the view function calls the model, which searches the database for data and returns it step by step.
The view function fills the returned data into the template's placeholders and finally returns the webpage to the user.
Reference Address: