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

查看:46
本文介绍了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 Framework),并且它们都将控制器定义为相同的东西:与该网站的特定主题相关的一堆函数,即用户帐户或类似的内容.我在此说明和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天全站免登陆