对 Django 声称的 MVC 感到困惑,它到底是什么? [英] Confused by Django's claim to MVC, what is it exactly?

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

问题描述

那么 Django 到底实现了什么?

好像有

模型观看次数模板

<块引用>

模型 = 数据库映射

视图 = 从通过模板对其进行建模和格式化

模板 = 根据视图给出的数据显示 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天全站免登陆