被Django声称对MVC感到困惑,究竟是什么呢? [英] Confused by Django's claim to MVC, what is it exactly?

查看:109
本文介绍了被Django声称对MVC感到困惑,究竟是什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



看起来像是有

 模型
视图
模板




模型=数据库映射



Views =从
模型获取相关数据,并通过模板格式化



Templates =根据视图给出的数据显示HTML


编辑:S. Lott在编辑中清除了很多以前的帖子,但我仍然希望听到其他反馈。谢谢!



这是正确的吗?真的好像Django和MVC几乎一样,只是通过调用它来混淆人们。

解决方案

Django的开发者有稍微非传统观点的MVC范式。他们在他们的常见问题解答中实际解决了这个问题,您可以阅读 。用自己的话说:


在我们对MVC的解释中,视图描述了呈现给用户的数据。数据看起来不一定如何,而是显示哪些数据。该视图描述了您看到的数据,而不是您看到的数据。这是一个微妙的区别。



所以,在我们的例子中,一个视图是特定URL的Python回调函数,因为回调函数描述了哪些数据被显示。



此外,将内容与演示文稿分离(这是模板进入的地方)是明智的。在Django中,视图描述了哪些数据被呈现,到一个模板,它描述了数据的呈现方式。



控制器在哪里适合?在Django的情况下,可能是框架本身:根据Django URL配置向相应视图发送请求的机器。



So what exactly is Django implementing?

Seems like there are

Models
Views
Templates

Models = Database mappings

Views = Grab relevant data from the models and formats it via templates

Templates = Display HTML depending on data given by Views

EDIT: S. Lott cleared a lot up with this in an edit to a previous post, but I would still like to hear other feedback. Thanks!

Is this correct? It really seems like Django is nowhere near the same as MVC and just confuses people by calling it that.

解决方案

Django's developers have a slightly non-traditional view on the MVC paradigm. They actually address this question in their FAQs, which you can read here. In their own words:

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.

这篇关于被Django声称对MVC感到困惑,究竟是什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆