其他 Pyramid 特征

Shell

pyramid有一个命令,可以将应用程序预加载到交互式python提示中。这对于调试或实验很有用。命令是“pshell”,类似于 Pylons 中的“paster shell”。

 1$ pshell development.ini
 2Python 2.6.5 (r265:79063, Apr 29 2010, 00:31:32)
 3[GCC 4.4.3] on linux2
 4Type "help" for more information.
 5
 6Environment:
 7  app          The WSGI application.
 8  registry     Active Pyramid registry.
 9  request      Active request object.
10  root         Root of the default resource tree.
11  root_factory Default root factory used to create `root`.
12
13>>>

它并没有像 Pylons 那样初始化那么多的全局变量,但是 apprequest 将是最有用的。

其他命令

其他可用命令:

  • proutes:列出应用程序的路由。(类似于 Pylons "paster routes。)

  • pviews: list the application's views.

  • ptweens: list the application's tweens.

  • prequest:加载应用程序,处理指定的URL,并在标准输出上打印响应正文。

形式

Pyramid does not include a form library. Pylons includes WebHelpers for form generation and FormEncode for validation and error messages. These work under Pyramid too. However, there's no built-in equivalent to Pylons' @validate 装饰者。Instead we recommend the "pyramid_simpleform" package, which replaces @validate with a more flexible structure.

有几个其他形式的类库,人们使用Pyramid。这些是定期讨论的 形式 Pyramid社区食谱的一部分。

WebHelpers

WebHelpers是一个第三方包,包含HTML标记生成器、文本函数、数字格式和统计函数,以及模板和视图中有用的其他通用函数。这是一个 Pylons 依赖,但在 Pyramid 是可选的。

这个 webhelpers.pylonslib subpackage does not work with Pyramid because it depends on Pylons' special globals. webhelpers.mimehelperwebhelpers.paginate 具有在其他框架下禁用的 Pylons 特定功能。尚未在python 3上测试webhelpers。

下一个版本的webhelpers可以作为不同的发行版(webhelpers2)发布,当前helpers的一个子集被移植到python 3。它可能会剥离分页和馈送生成器来分离分配。

事件

事件框架提供了Hook,您可以在其中将自己的代码插入请求处理序列,类似于Apache模块的工作方式。它标准化了一些在 Pylons 中特别提供或根本不提供的定制。要使用它,请为中的一个事件类型编写回调函数 pyramid.eventsApplicationCreatedContextFoundNewResponseBeforeRender . 回调接受特定于事件类型的事件参数。您可以向注册活动 @asubscriberconfig.add_subscriber() . Akhet演示有一些例子。

有关详细信息,请参阅:

URL生成

Pyramid 不附带等同于“pylons.url”的URL生成器。请求对象上的各个方法可用于生成特定类型的URL。其中,route-url包含按名称生成路由的正常情况:

request.route_url("route_name", variable1="value1")
request.route_path("route_name", variable1="value1")
request.route_url("search", _query={"q": "search term"}, _anchor="results")

route_url generates an absolute URL, while route_path _query _anchor _scheme_host_port_app_url .

使用这些方法而不是对URL进行硬编码的好处是,它会自动添加应用程序前缀(如果应用程序安装在子URL上,则前缀可能比“/”多)。

您还可以传递其他位置参数,它们将作为组件附加到URL。这对于URL调度不是很有用,它更像是一种遍历。

If the route is defined with a 前置发生器, it will be called with the positional and keyword arguments, and can modify them before the URL is generated.

url variable for your templates, using an event subscriber. Then you can do things like this:

1url.route("route_name")          # Generate URL by route name.
2url("route_name")                # The same.
3url.app                          # The application's top-level URL.
4url.current()                    # The current request URL. (Used to
5                                 # link to the same URL with different
6                                 # match variables or query params.)

您还可以自定义它以执行以下操作:

url.static("images/logo.png")
url.image("logo.png")            # Serve an image from the images dir.
url.deform("...")                # Static file in the Deform package.

如果“url”对你来说太长,你甚至可以把它命名为“u”!

实用脚本

Writing a Script .

测试

Pyramid使为视图编写单元测试变得更容易。

(XXX Need a comparison example.)

国际化

Pyramid 支持国际化。目前,它主要是记录 Chameleon 模板,而不是Mako。

更高层次的框架

Pyramid 提供了一个灵活的基础来构建更高层次的框架。已经写了好几个。还有应用脚手架和柏油球。

  • Kotti_uu是一个内容管理系统,既可以开箱即用,也可以扩展。

  • ptah_u是一个框架,旨在拥有和django一样多的功能。(但没有小马,也没有牛铃。)它有一个最小的CMS组件。

  • 胡夫是 Pyramid 的一套脚手架和公用设施。

  • 我们之前提到的Akhet_uu演示。它是tarball中的一个工作应用程序,您可以从中复制代码。

在相反的极端,您可以在14行Python中制作一个小的 Pyramid 应用程序,而无需使用脚手架。 Pyramid 手册有一个例子: Hello World . 这在 Pylons 上是不可能的——至少,在没有严重扭曲的情况下是不可能的。