Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction of Django MTV mode

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the detailed introduction of Django MTV mode", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "the detailed introduction of Django MTV mode"!

MVC mode

MVC, full name Model View Controller, is a software architecture pattern in software engineering, which divides the software system into three basic parts: Model, View and Controller. It has the advantages of low coupling, high reusability and low life cycle cost.

MVT mode

Model: the data storage layer, which handles all transactions related to data, such as how to access, how to verify validity, what behaviors are included, and the relationships between the data. Responsible for business objects and database objects (orm).

Templates: presentation layer that handles performance-related decisions, such as how to display them in pages or other types of documents.

As a Web framework, Django needs a convenient way to generate HTML pages dynamically, so there is the concept of template. The template contains some code for the required HTML and some special syntax, which is used to describe how to dynamically insert the data passed by the view into the HTML page.

Django can be configured with one or more template engines (or even 0, such as separation of front and rear, Django only provides API interface without using template engine), template engine has Django template language (DTL) and Jinja2. Django template language is one of the built-in functions of Django, and Jinja2 is a popular template language of Python at present.

View: business logic layer, access model and access to appropriate template related logic, model and template bridge. It is mainly responsible for processing user requests and generating corresponding content, which is then displayed on pages or other types of documents. It can also be understood that the view is the C part (controller) of the MVC architecture, which mainly deals with functional and business logic. We are used to using view functions to handle HTTP requests, recording the definition of def functions in the view, which is called FBV (Function Base Views).

(quoted from: https://zhuanlan.zhihu.com/p/362268440)

The Django framework is based on the MTV pattern, which divides the development task into three parts:

Model: responsible for the relational mapping (ORM) of business objects and databases, which is equivalent to Model in the MVC schema.

Template: responsible for how to present the page to the user (HTML), which is similar to the view in MVC.

View: responsible for the business logic and invoking Model and Template when appropriate. It's the same as Controller in MVC.

First of all, we need to know: what is MVT?

It was also mentioned yesterday:

M is spelled as Model, which has the same function as M in MVC. It is responsible for interacting with database and data processing.

V is spelled as View, which has the same function as C in MVC. It receives the request, processes the business, and returns the reply.

T is spelled as Template, which has the same function as V in MVC, and is responsible for encapsulating the html to be returned by construction.

MVT is a cutting-edge software architecture model, which has efficient ORM (Object Relation Mapping) data operations. The object [object] in the program is associated with the table [relation] in the database, and the process of the relationship [mapping] is called ORM.

Why use the MVT structure?

1. Easy to develop with high cohesion and low coupling

In the actual development process, the original version needs to be iterated according to different requirements. MVT separates the database operation, request processing and page layout, which facilitates the update of the code, does not need to affect the whole body, and is easy to maintain the code.

two。 Improve development efficiency

Achieve the separation of the front and back end, easy to integrate the code. It is convenient for users to divide their work and cooperate. It also improves the reuse, facilitates the reuse of similar parts, and improves the development efficiency.

So, do you still remember the phrase "Django framework follows MVC design"? What is MVC?

M stands for Model, which is mainly used to encapsulate the database layer.

V stands for View, which is used to show the result to the user

C stands for Controller, which is the core and is used to process requests, get data, and return results.

With regard to MVC, there are three points to note:

1. MVC frameworks are available in most development languages

The core idea of 2.MVC framework is: decoupling

3. It can reduce the coupling between the functional modules, facilitate the change, reconstruct the code more easily, and maximize the code reuse.

Finally, the Django architecture pattern:

MVT architecture mode: on the basis of MVC, the controller controller is encapsulated and the module function [Templates] is added to realize the front-end web page [Html].

MVC architecture mode: data model- > View views- > data interaction-> Server-> Controller controller- > calls to corresponding functions.

At this point, I believe you have a deeper understanding of the "detailed introduction of Django MTV mode", you might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report