加入星计划,您可以享受以下权益:

  • 创作内容快速变现
  • 行业影响力扩散
  • 作品版权保护
  • 300W+ 专业用户
  • 1.5W+ 优质创作者
  • 5000+ 长期合作伙伴
立即加入
  • 正文
    • MTV模型
  • 相关推荐
  • 电子产业图谱
申请入驻 产业图谱

Django让web开发更简单(二):了解MVT模式

23小时前
252
阅读需 6 分钟
加入交流群
扫码加入
获取工程师必备礼包
参与热点资讯讨论

前情提示

上一章节,我们“重头到尾”把前后端不分离的开发模型MTV(也有叫MVT)撸了一遍,发现跟java的MVC模型(这里不讲解)很像。

官方文档不采用MVC的说法,而采用MTV的说法,原因也有说明,以下是原话:

(地址:https://docs.djangoproject.com/en/3.1/faq/general/)

Django appears to be a MVC framework, but you call the Controller the “view”, and the View the “template”. How come you don’t use the standard names?¶
Well, the standard names are debatable.

In our interpretation of MVC, the “view” describes the data that gets presented to the user. It’s not necessarily how the data looks, but which data is presented. The view describes which data you see, not how you see it. It’s a subtle distinction.

So, in our case, a “view” is the Python callback function for a particular URL, because that callback function describes which data is presented.

Furthermore, it’s sensible to separate content from presentation – which is where templates come in. In Django, a “view” describes which data is presented, but a view normally delegates to a template, which describes how the data is presented.

Where does the “controller” fit in, then? In Django’s case, it’s probably the framework itself: the machinery that sends a request to the appropriate view, according to the Django URL configuration.

If you’re hungry for acronyms, you might say that Django is a “MTV” framework – that is, “model”, “template”, and “view.” That breakdown makes much more sense.

At the end of the day, it comes down to getting stuff done. And, regardless of how things are named, Django gets stuff done in a way that’s most logical to us.

从定义上,django开发团队,觉得自己更严谨。

定义一个事物,是一件很重要的事情。如果你定义了一个东西,大家都这么叫,以后说什么都是占据主动权,因为你是鼻祖!所以,定义是个伟大的事情,让你具有降维打击的主动权。

此为,前话。

MTV模型

“MTV” framework – that is, “model”, “template”, and “view.”

模型(model):用于操作数据库
模板(template):用于展示数据
视图(view):用于控制业务逻辑

工作流程图如下:

在这里插入图片描述

了解django的工作机制,可以更好的进行开发,即以一个全局观的思维去看待django的,你就知道要开发和运行django服务,需要做什么了。

相关推荐

电子产业图谱