前情提示
上一章节,我们“重头到尾”把前后端不分离的开发模型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服务,需要做什么了。