在Python金字塔路由配置中使用查询字符串

2024-10-01 17:37:21 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我要做的非常具体的事情,所以我开始描述它是什么:

  • 一个金字塔应用程序,为http://localhost:6543/path/to/myplot/plot001.png等绘图提供服务
  • 如果该图不可用,则提供另一个图像(工作.png)在
  • 另一部分是deform视图,它提供了一个HTML表单来输入绘图的配置,比如:http://localhost:6543/path/to/myplot/plot001.png?action=edit。注意这里的查询字符串“action=edit”。在
  • 配置包括数据文件、模板等
  • 表单有save(保存配置)和render按钮(http://localhost:6543/path/to/myplot/plot001.png?操作=渲染)。将结果渲染为png文件,然后以静态方式使用该文件。在

我已经解决了所有的问题,比如使用Matplotlib进行渲染等,但我对金字塔和变形还不熟悉。我也有一个工作视图,用于从文件打印。变形形式也可以。目前,我不清楚如何最好地构建ULRs来区分服务、编辑和呈现用例。我猜在金字塔谈话中,这意味着如何配置服务视图和编辑视图的路由。在

__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):
...

我的金字塔手册我找不到参考如何设置参数的路线。我想一个指向一些文档或示例的指针就足够了,我可以自己找出细节。谢谢您!在


Tags: topathnameview视图configlocalhosthttp
3条回答

有两种方法可以做到这一点,这取决于您喜欢将代码分隔开的方式。在

  1. 将所有逻辑放入您的视图中,用request.GET.get('action')上的“if”语句分隔。在

    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. 使用金字塔的视图查找机制注册多个视图并在它们之间进行委派。在

    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上的不同类型的请求使用不同的视图。在

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

初始版本:

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

在视图.py公司名称:

^{pr2}$

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

编辑:

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

初始版本:

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

更有效的方法是在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

或使用查询字符串

^{pr2}$

相关问题 更多 >

    热门问题