Pyramid(Pylons)的新文件/目录结构让我有些困惑
我已经在 Pylons 中开发了一段时间,最近了解到它们正在与另一个框架合并来创建 Pyramid。
我一直在查看示例代码 查看差异,这导致了一些混乱...
例如,控制器已被视图取代。不是一个大问题...但我发现有趣的是没有这些目录。它只是一个文件:views.py
。
这个新的 MVC 结构如何工作?我是否将所有操作都写入这一个文件中?当我有类似命名的操作(例如多个索引)时,这可能会变得相当烦人:/
你能给我指出一些关于如何使用这个框架的好的教程/文档吗?
I've been developing in Pylons for a little while now and have recently learned they're merging with another framework to create Pyramid.
I've been looking over example code to see the differences and it's causing a bit of confusion...
For example, Controllers have been replaced by Views. Not a big problem... But what I find interesting is there's no directories for these. It's simply one file: views.py
.
How does this new MVC structure work? Do I write all my actions into this one file? That could get rather annoying when I have similarly named actions (multiple indexes, for example) :/
Could you point me in the direction of some good tutorials/documentation on how to use this framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于各种与视图相关的配置方法(config.add_view、config.add_handler)要求您传递点分名称作为要用作视图或处理程序的类或函数,因此您可以根据自己的喜好安排代码。
例如,如果您的项目包名称为
myproject
,并且想要将所有视图排列在名为“views”的myproject
包内的 Python 子包中(请参阅 http://docs.python.org/tutorial/modules.html#packages)而不是单个视图文件,您可以:在
mypackage
包中创建一个views
目录。将现有的
views.py
文件移动到新views
目录中的文件,例如,blog.py
。在新的
views
目录中创建一个名为__init__.py
的文件(可以为空,这只是告诉 Python
views
目录是一个 package。然后更改
myproject
项目的__init__.py
(不是您刚刚在myproject
中创建的__init__.py
code>views 目录,即其父目录中的目录),例如:config.add_handler('myhandler', '/my/handler', handler='mypackage.views.MyHandler')
至:
config.add_handler('myhandler', '/my/handler', handler='mypackage.views.blog.MyHandler')
然后您可以继续将文件添加到
view
目录,并通过作为handler=
或view=
传递的点分名称引用这些文件中的视图或处理程序类/函数。Since the various view-related configuration methods (config.add_view, config.add_handler) require you to pass a dotted name as the class or function to be used as a view or handler, you can arrange your code however you like.
For example, if your project package name were
myproject
and wanted to arrange all your views in a Python subpackage within themyproject
package named "views" (see http://docs.python.org/tutorial/modules.html#packages) instead of a single views file, you might:Create a
views
directory inside yourmypackage
package.Move the existing
views.py
file to a file inside the newviews
directory named, say,blog.py
.Create a file within the new
views
directory named__init__.py
(it can be empty,this just tells Python that the
views
directory is a package.Then change the
__init__.py
of yourmyproject
project (not the__init__.py
you just created in theviews
directory, the one in its parent directory) from something like:config.add_handler('myhandler', '/my/handler', handler='mypackage.views.MyHandler')
To:
config.add_handler('myhandler', '/my/handler', handler='mypackage.views.blog.MyHandler')
You can then continue to add files to the
views
directory, and refer to views or handler classes/functions within those files via the dotted name passed ashandler=
orview=
.这是一个应该非常简单的答案。这个问题是在 Pyramid 1.3 尚未发布时提出的。所以忘记 python 处理程序吧,因为新的装饰器现在做得非常好。
但首先:金字塔没有任何共同的结构。如果您愿意,您可以在一个文件中编写整个应用程序。换句话说,如果您喜欢塔架的结构,您可以采用它。如果您更喜欢设置自己的结构,那就去做吧。
如果您的网站不需要多个文件,那么......就去吧!您真正需要的只是它有效。
我个人有这样的结构,
我的视图包有时可能会分成许多文件,我在其中按资源类型对视图进行分组。
如果你碰巧使用上下文来匹配视图而不是路由。您可以使用 view_defaults< 做一些非常好的事情/a> 和 view_config。
view_defaults
为类设置一些默认值,view_config
使用view_defaults
提供的默认值(如果存在)为 def 设置更多配置。Here is one answer that should be pretty straight forward. This question was asked when Pyramid 1.3 wasn't yet out. So forget about python handlers since the new decorator do a pretty good job now.
But just to start: Pyramid doesn't have any common structure. You could possibly write a whole app in one single file if you wanted. In other words, if you liked how pylons was structured, you can go with it. If you prefer to setup your own structure then go for it.
If your site doesn't need more than one file then...GO FOR IT!!! All you really need is that it works.
I personally have a structure like that
My view package might sometimes be splitted up in many files where I'd group views by ResourceType.
If you happen to use context to match views instead of routes. You can do some pretty nice things with view_defaults and view_config.
view_defaults
sets some default for the class, andview_config
sets some more configurations for the defs using defaults provided byview_defaults
if present.