6

这对我正在尝试做的事情非常具体,所以我开始描述它是什么:

  • 一个 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 手册中,我找不到如何在路线中设置参数的参考。我想指向一些文档或示例的指针就足够了,我可以自己弄清楚细节。谢谢!

4

3 回答 3

12

有两种方法可以做到这一点,具体取决于您喜欢分离代码的方式。

  1. 将所有逻辑放入您的视图中,由 'if' 语句分隔request.GET.get('action')

    config.add_route('plot', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')
    config.scan()
    
    @view_config(route_name='plot')
    def plot_view(request):
        action = request.GET('action')
        if action == 'edit':
            # do something
            return render_to_response('bunseki:templates/form.pt', {}, request)
    
        # return the png
    
  2. 使用 Pyramid 的视图查找机制注册多个视图并在它们之间进行委托。

    config.add_route('plot', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')
    config.scan()
    
    @view_config(route_name='plot')
    def plot_image_view(request):
        # return the plot image
    
    @view_config(route_name='plot', request_param='action=edit',
                 renderer='bunseki:templates/form.pt')
    def edit_plot_view(request):
        # edit the plot
        return {}
    
    # etc..
    

希望这可以帮助。这是注册单个 url 模式并针对该 url 上不同类型的请求使用不同视图的一个很好的示例。

于 2011-08-11T16:36:09.567 回答
1

我不确定您是否可以contex=Root在这种情况下使用,但您要求的可能是matchdict.

__init__.py:

config.add_route('serve_route', 
    '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')

视图.py:

@view_config(route_name='serve_route')
def plot_view(request):
    project_name = request.matchdict['project_name']
    action = request.params.get('action', None)

http://docs.pylonsproject.org/projects/pyramid/1.1/narr/urldispatch.html#matchdict

编辑:

如果您的问题是关于路由的一般性问题,您应该为每个操作创建一个路由,以使您的视图函数的代码更短、更清晰。例如,如果您想编辑和渲染,您的路线可能如下所示:

__init__.py:

config.add_route('render_plot',
    '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')
config.add_route('edit_plot',
    '/{project_name}/testruns/{testrun_name}/plots/{plot_name}/edit')

视图.py:

@view_config('render_plot')
def render(request):
    pass

@view_config('edit_plot', renderer='bunseki:templates/form.pt')
def edit(request):
    pass
于 2011-08-01T13:03:00.257 回答
0

更有效的方法是在 url 中指定操作。您甚至可以在相同或多个路由名称上提供不同的操作。

config.add_route('serve_route', '/{project_name}/testruns/{testrun_name}/plots/{action}/{plot_name}.png')

views.py
@view_config(context=Root, route_name='serve_route', action='view')
def plot_view(request):
    pass

或使用查询字符串

`config.add_route('serve_route', 
    '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')

views.py
@view_config(context=Root, route_name='serve_route')
def plot_view(request):
    try:
       action = getattr(self._request.GET, 'action')
    except AttributeError:
       raise 
于 2016-12-19T16:46:49.263 回答