Django 简明教程

Django – MVT Architecture

大多数 Web 框架都实现了 MVC(模型-视图-控制器)架构。Django 使用 MVC 的一种变体,并且称它为 MVT(代表模型-视图-模板)架构。

Most of the web frameworks implement the MVC (Model-View-Controller) architecture. Django uses a variation of MVC and calls it the MVT (stands for Model-View-Template) architecture.

The Advantage of Using a Web Framework

一般来说,一个软件框架是一个标准、可重用的软件平台,有助于快速开发软件应用程序。与此相反,一个 web framework (也称作 Web 应用程序框架)比如 Django 提供了构建 Web 应用程序、API 和 Web 服务所需的通用功能。

A software framework, in general, is a standard, reusable software platform that facilitates rapid development of software applications. In contrast, a web framework (also called web application framework) such as Django provides a generic functionality needed for building web applications, APIs and web services.

采用 Web 框架的主要优点在于,它提供开箱即用的支持来执行 Web 开发过程中的常见操作。例如,您可以轻松地将您的应用程序连接至数据库。

The main advantage of employing a web framework is that it provides out-of-the-box support to perform common operations in the process of web development. For example, you can easily connect your application to the databases.

通常,该框架能够更有效地处理诸如会话管理之类的任务。同样地,它与模板工具集成在一起,以便在 Web 页面上呈现动态内容。

Usually, the framework handles tasks such as session management much more efficiently. Likewise, it integrates with templating tools to render dynamic content on web page.

The MVC Architecture

这种设计模式将整个 Web 应用程序开发过程分成了三层。下图解释了这三层之间的相互作用。

This design pattern separates the entire process of web application development in three layers. The following diagram explains the interplay of these three layers.

django mvc architecture

在 MVC 方法中,用户请求会被控制器拦截。它与视图层与模型层协调,以将适当的响应发回客户端。

In the MVC approach, the user requests are intercepted by the controller. It coordinates with the view layer and the model layer to send the appropriate response back to the client.

The Model Layer

模型被称为最低层,这意味着它负责维护数据。它处理数据。

The Model is known as the lowest level which means it is responsible for maintaining the data. It handles data.

模型层连接至数据库。它响应控制器的请求,因为控制器本身永远不会直接与数据库通信。模型往返式地与数据库通信,然后将所需的数据提供给控制器。

The model layer is connected to the database. It responds to the controller requests because the controller never talks to the database by itself. The model talks to the database back and forth and then it gives the needed data to the controller.

模型负责数据定义、其处理逻辑与后端数据库的交互。

The model is responsible for data definitions, its processing logic and interaction with the backend database.

The View Layer

视图是应用程序的表现层。它负责结果的显示与格式化并将结果发送至控制器,进而由控制器作为应用程序响应将其重定向到客户端。

The View is the presentation layer of the application. It takes care of the placement and formatting of the result and sends it to the controller, which in turn, redirects it to the client as the application’s response.

数据表示由视图组件完成。它实际上为用户生成了 UI 或用户界面。所以,在 Web 应用程序中,当您想到视图组件时,只要考虑 HTML/CSS 这部分。

Data representation is done by the view component. It actually generates the UI or user interface for the user. So, at web applications, when you think of the View component, just think the HTML/CSS part.

视图由模型组件收集的数据创建,但这些数据不会直接获取,而是通过控制器获取,因此视图只与控制器通信。

Views are created by the data which is collected by the model component but these data aren’t taken directly but through the controller, so the view only speaks to the controller.

The Controller Layer

它与视图层与模型层协调,以将适当的响应发回客户端。

It coordinates with the View layer and the model layer to send the appropriate response back to the client.

控制器层接收来自客户端的请求,并将其转发到模型层。模型层更新数据并发送回控制器。控制器更新视图并将响应发回用户。

The Controller layer receives the request from the client, and forwards it to the model layer. The Model layer updates the data and sends back to the controller. The Controller updates the view and sends back the response to the user.

The MVT Architecture

Django 框架采用了 MVT 方法。这是 MVC 方法的一个微小变体。缩写 MVT 代表模型、视图和模板。

The Django framework adapts a MVT approach. It is a slight variation of the MVC approach. The acronym MVT stands for Model, View and Template.

在这里,模型同样是应用程序的数据层。视图实际上是承担处理逻辑的层。模板是表现层。

Here too, the Model is the data layer of the application. The View is in fact the layer that undertakes the processing logic. The Template is the presentation layer.

django mvt architecture

Components of a Django Application

Django 应用程序包含以下组件 −

A Django application consists of the following components −

  1. URL dispatcher

  2. View

  3. Model

  4. Template

The URL Dispatcher

Django 的 URL 调度机制等同于 MVC 架构中的控制器。Django 项目的包文件夹中的 urls.py 模块作为该调度器。它定义了 URL 模式。每个 URL 模式都会映射到一个视图函数,当发现客户端请求的 URL 与其匹配时调用该函数。

Django’s URL dispatcher mechanism is equivalent to Controller in the MVC architecture. The urls.py module in the Django project’s package folder acts as the dispatcher. It defines the URL patterns. Each URL pattern is mapped with a view function to be invoked when the client’s request URL is found to be matching with it.

每个应用中定义的 URL 模式都包含其中,位于该项目之下。

The URL patterns defined in each app under the project are also included in it.

当服务器以客户端 URL 的形式接收请求时,调度器会将其模式与 urls.py 中提供的模式进行匹配,并将应用程序流路由到其关联的视图。

When the server receives a request in the form of client URL, the dispatcher matches its pattern with the patterns available in the urls.py and routes the flow of the application towards its associated view.

The View Function

视图函数读取客户端请求中包含的路径参数、查询参数和主体参数。它使用这些数据与模型进行交互,以执行 CRUD 操作(如果需要)。

The View function reads the path parameters, query parameters and the body parameters included in the client’s request. It uses this data to interact with the models to perform CRUD operations, if required.

The Model Class

模型是一个 Python 类。Django 使用模型类的属性来构造具有匹配结构的数据库表。Django 的对象关系映射器帮助以面向对象的方式执行 CRUD 操作,而不是调用 SQL 查询。

A Model is a Python class. Django uses the attributes of the Model class to construct a database table of a matching structure. Django’s Object Relational Mapper helps in performing CRUD operations in an object oriented way instead of invoking SQL queries.

视图将来自客户端和模型的数据用作其响应的模板。

The View uses the data from the client as well as the model and renders its response in the form of a template.

Template

模板是一个网页,其中 HTML 脚本与 Django 模板语言的代码块交织在一起。

A Template is a web page in which HTML script is interspersed with the code blocks of Django Template Language.

Django 的模板处理器使用从视图接收到的任何上下文数据插入到这些块中,以便制定动态响应。然后,视图向用户返回响应。

Django’s template processor uses any context data received from the View is inserted in these blocks so that a dynamic response is formulated. The View in turn returns the response to the user.

这就是 Django 的 MVT 架构在 Web 应用程序中处理请求-响应周期的方式。

This is how Django’s MVT architecture handles the request-response cycle in a web application.