Pyramid(Pylons)的新文件/目录结构让我有些困惑

发布于 2024-10-01 05:49:08 字数 405 浏览 5 评论 0原文

我已经在 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 技术交流群。

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

发布评论

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

评论(2

最终幸福 2024-10-08 05:49:08

由于各种与视图相关的配置方法(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 the myproject 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 your mypackage package.

  • Move the existing views.py file to a file inside the new views 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 your myproject project (not the __init__.py you just created in the views 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 as handler= or view=.

秉烛思 2024-10-08 05:49:08

这是一个应该非常简单的答案。这个问题是在 Pyramid 1.3 尚未发布时提出的。所以忘记 python 处理程序吧,因为新的装饰器现在做得非常好。

但首先:金字塔没有任何共同的结构。如果您愿意,您可以在一个文件中编写整个应用程序。换句话说,如果您喜欢塔架的结构,您可以采用它。如果您更喜欢设置自己的结构,那就去做吧。

如果您的网站不需要多个文件,那么......就去吧!您真正需要的只是它有效。

我个人有这样的结构,

- root
    - __init__.py # all setup goes there
    - security.py # where functions related to ACL and group_finder
    - models.py or models/ # where all my models go
    - views.py or views/   # where all my views go 
    - templates
       - modelname
          - all template related to this resource type

    - scripts # where I put my scripts like backup etc
    - lib # all utilities goes there
    - subscribers # where all events are defined

我的视图包有时可能会分成许多文件,我在其中按资源类型对视图进行分组。

如果你碰巧使用上下文来匹配视图而不是路由。您可以使用 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

- root
    - __init__.py # all setup goes there
    - security.py # where functions related to ACL and group_finder
    - models.py or models/ # where all my models go
    - views.py or views/   # where all my views go 
    - templates
       - modelname
          - all template related to this resource type

    - scripts # where I put my scripts like backup etc
    - lib # all utilities goes there
    - subscribers # where all events are defined

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, and view_config sets some more configurations for the defs using defaults provided by view_defaults if present.

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