将思维从 CakePHP 转移到 Django - 一个整体视图文件?

发布于 2024-09-13 09:03:22 字数 959 浏览 2 评论 0原文

我正在尝试开始使用 Django,并且之前曾使用过 CakePHP,所以我的 MVC 背景就是由此而来。我知道 Django 的 MTV 架构略有不同,并且对整体模型文件很满意 - 一个文件中的多个类我可以很好地处理。

但我对如何处理视图感到困惑(这大致类似于 MVC 中的控制器,对吗?)。我见过的示例只有一个 views.py,它具有 index()view() 等方法。但是如果我有一群用户创建并拥有可以共享的小部件,例如,我想要为用户模型运行 view()/users/view ,和为小部件模型运行 view()/widgets/view

我没有看到任何方法可以将它们分开,也不知道正确/传统/正确的方法是什么。我可能也无法理解 Django 的做事方式。 view.py 中是否应该有 user_viewwidget_view 方法?这看起来很笨拙。

或者我应该有包含 index()view()user_view.py 甚至 user/view.py >?我可以从 URL 路由中引用这些内容吗? Django 之类的事情通常是如何完成的?

这最终可能与这个答案相关(甚至可以解决) ,但我更多地问的是思考这些事情的惯例和正确方法是什么的问题。

此外,文档/示例不应该更清楚这一点吗?到目前为止,这些文档给我留下了深刻的印象,但我很确定大多数网络应用程序都会处理多个“对象”,而且在我看来,这种情况会经常出现。

I'm trying to get started with Django, and have previously worked with CakePHP, and so my MVC background comes out of that. I'm aware of Django's slightly different MTV architecture, and am fine with the monolithic model files - multiple classes in one file I can handle just fine.

But I'm confused about how to do the views (which are roughly analagous to controllers in MVC, correct?). The examples I've seen just have one views.py that has methods like index(), view(), etc. But if I have a bunch of users that create and own widgets that they can share, for example, I want to have /users/view that runs view() for the users model, and /widgets/view that runs view() for the widgets model.

I don't see any way to separate those out, and don't know what the correct/conventional/right way is to do so. I may just be having trouble wrapping my head around Django's way of doing things, too. Should I have methods in view.py that are user_view and widget_view? That seems very clunky.

Or should I have user_view.py or even user/view.py that contains index() and view()? Could I reference those from the URL routing? How are things generally done with Django and this kind of thing?

This may ultimately be related to (or even solved by) this answer, but I'm asking more as a question of what convention and the right way to think about such things is.

Additionally, shouldn't the docs/examples be clearer on this? I've been impressed by the docs thus far, but I'm pretty sure most web apps will deal with more than one "object," and it seems to me that this would come up pretty often.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小矜持 2024-09-20 09:03:22

Python 视图文件只是 Python 模块。视图本身只是可以存在于您喜欢的任何地方的函数 - 该模块甚至不必称为 views.py。 urlconf(在 urls.py 中)可以引用任何地方的视图。

一种明显的分离方法是分成单独的应用程序,文档中对此进行了详细介绍 - 您还可以为每个应用程序使用单独的 urls.py 文件,并在主站点级 URL 中使用 include 。 py 以包含所有子文件。

但是没有什么可以阻止您将单个应用程序中的视图细分为多个文件 - 例如,通过创建一个 views 模块,其中包含一个(空白)__init__.py 和您可以根据需要查看许多其他文件。

或者,如果您确实拥有仅与特定模型关联的视图 - 并且您会惊讶地发现这种情况很少 - 再次,您可以在模型类本身上创建视图类方法。视图所要做的就是接受请求和任何其他参数,并返回响应。

Python view files are just Python modules. The views themselves are just functions that can live anywhere you like - the module doesn't even have to be called views.py. The urlconf (in urls.py) can refer to views anywhere at all.

One obvious way of separating things out is into separate applications, which is covered well in the documentation - you can also have separate urls.py files for each app and use include in the main site-level urls.py to include all the sub-files.

But there's nothing to stop you sub-dividing the views in a single app into multiple files - eg by creating a views module, containing a (blank) __init__.py and as many other view files as you like.

Or, if you really do have views associated only with a particular model - and you'd be surprised how seldom that is the case - again, you could make your views classmethods on the model class itself. All a view has to do is to accept a request, and any other parameters, and return a response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文