【发布时间】:2011-08-01 10:10:56
【问题描述】:
这对我正在尝试做的事情非常具体,所以我开始描述它是什么:
- 一个 Pyramid 应用程序,提供像 http://localhost:6543/path/to/myplot/plot001.png 这样的情节
- 如果绘图不可用,则提供另一张图片 (work.png)
- 另一部分是变形视图,它提供了一个 HTML 表单来输入绘图的配置,例如:http://localhost:6543/path/to/myplot/plot001.png?action=edit。请注意此处的查询字符串“action=edit”。
- 配置由数据文件、模板等组成。
- 表单有保存(保存配置)和渲染按钮(http://localhost:6543/path/to/myplot/plot001.png?action=render)。将结果渲染成 png 文件,然后以静态方式使用。
我想出了所有的部分,比如使用 Matplotlib 等进行渲染,但我对 Pyramid 和 Deform 是新手。我还有一个工作视图,可以从文件中提供绘图。变形形式也有效。目前,我不清楚如何最好地构建 ULR 以区分服务、编辑和渲染用例。我猜在 Pyramid talk 中这意味着如何配置 serve_view 和 edit_view 的路由。
__init__.py:
config.add_route('serve_route',
'/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')
config.add_route('edit_route',
'/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')
# can I use query strings like "?action=edit" here to distinguish the difference?
views.py:
@view_config(context=Root, route_name='serve_route')
def plot_view(context, request):
...
@view_config(context=Root, renderer='bunseki:templates/form.pt', route_name='edit_route')
def edit_view(request):
...
我在 Pyramid 手册中找不到参考如何在路线中设置参数。我想指向一些文档或示例的指针就足够了,我可以自己弄清楚细节。谢谢!
【问题讨论】:
-
Pyramid 有一种他们称之为 Multidict 的机制。我认为这是访问查询字符串的方法。我会将两个视图绘图和编辑合并为一个,并使用以下内容进行区分: if 'edit' in request.GET.getall('action'): # edit the configuration
-
你在上面的评论中说的很好,这可能是我会这样做的方式;您可以交替将自定义谓词添加到 add_route 语句,这将消除查询字符串上的两个路由的歧义以进行匹配。见docs.pylonsproject.org/projects/pyramid/1.1/narr/…
标签: python forms routes pyramid deform