Django 中的 MVC 模式 [英] MVC pattern in django

查看:14
本文介绍了Django 中的 MVC 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经困扰了我一段时间了.我读过关于这个话题,但似乎没有什么能清除我的想法.我知道他们称视图模板和模型模型为好,我真正不知道的是控制器在哪里.django 所谓的视图在我看来更像是控制器的动作/方法/功能,而不是控制器本身,但在我阅读的任何地方,我都发现所谓的视图控制器等效.

This issue has been tormenting me for a while already. I've read about this topic but nothing seems to clear my thoughts. I understand that they call the views templates, and the models models as well, what I don't really get is where the controllers are. What django calls views seem to me more like actions/methods/functions of a controller than a controller itself, but anywhere I read, I find that supposed view-controller equivalency.

我以前使用过 MVC 框架(ASP.NET MVC3、Ruby on Rails、PHP Laravel 框架),它们都将控制器定义为相同的东西:一堆与站点特定主题相关的函数,即用户帐户或类似的东西.我在此描述和 django 功能之间找到的最佳等效项是应用程序,但当然我错了,因为大量的人和文档相反.

I've worked with MVC frameworks before (ASP.NET MVC3, Ruby on Rails, PHP Laravel Framework), and they all define the controllers as the same thing: a bunch of functions related to a specific topic of the site, namely user accounts or something like that. The best equivalency that I find between this description and django features are the apps, but of course I'm wrong due to the huge amount of people and documentation going the other way.

有人可以帮我解决这个问题吗?我的想法有意义吗?我是否在这里遗漏了一些重要的东西,然后我无法正确理解这些概念?

Could anybody help me with this? Does my mindset make any sense? Am I missing something essential here and then I can't get these concepts right?

推荐答案

将 MVC 等设计模式视为牢不可破的规则是错误的.它们确实不是:有各种实现它们的方法,它们或多或少地符合描述.

It's a mistake to think of design patterns like MVC as unbreakable rules. They really aren't: there are all sorts of ways of implementing them, which comply with the description to a greater or lesser extent.

在 Python 中尤其如此,其中 指导原则之一 是务实胜过纯洁"——换句话说,做有效的事.

This is especially the case in Python, where one of the guiding principles is "practicality beats purity" - in other words, do what works.

无论如何,Django 并没有声称自己是一个 MVC 框架.相反,文档将其描述为 MTV: 模型、模板、视图.毕竟,在设计模式的世界之外,每个人都将带有变量和流控制语法的 HTML 文件"称为模板,而不是视图.

In any case, Django makes no claim to be an MVC framework. On the contrary, the documentation describes it as MTV: model, template, view. After all, outside the world of design patterns everyone calls "an HTML file with syntax for variables and flow control" a template, not a view.

(该 FAQ 条目还为您的问题提供了一个可能的答案:控制器就是框架本身.但它继续强调,试图强加于这些定义是错误的.)

(That FAQ entry also offers a possible answer to your question: the controller is the framework itself. But it goes on to emphasise that it's a mistake to try to shoehorn into these definitions.)

这篇关于Django 中的 MVC 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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