在遍历中定义回退视图

2024-05-19 11:29:57 发布

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

考虑以下金字塔应用程序:

from pyramid.config import Configurator

class Foo(dict):
    pass

def make_root(request):
    return {'foo': Foo()}

def foo(request):
    return request.subpath

def bar(request):
    return {"whoami": "bar", "subpath": request.subpath}

def start(global_config, **settings):
    config = Configurator(settings=settings)
    config.set_root_factory(make_root)
    config.add_view(foo, context=Foo, renderer="json")
    config.add_view(bar, name="bar", context=Foo, renderer="json")
    return config.make_wsgi_app()

此应用程序使用遍历并对/foo/foo/bar进行响应。我想知道,当遍历/foo/booarns时,在Traversal决定返回404之前,是否有一些地方可以钩住。类似于默认视图或回退视图:

^{pr2}$

然后,只要路径的第二个组件没有绑定到任何其他视图,在相同的上下文中,路径组件仍然可用作request.view_name和{},就会调用这个视图。在


Tags: view视图config应用程序makereturnsettingsfoo
3条回答

According to the docs,只需为名称指定一个空字符串:

config.add_view(any_other, name="", context=Foo, ...)

当金字塔无法将URL映射到视图代码时,它会触发“找不到视图”。默认的“找不到视图”可以通过应用程序配置覆盖,方法如下:

from pyramid.view import notfound_view_config

@notfound_view_config()
def notfound(request):
    return Response('Not Found, dude', status='404 Not Found')

def main(globals, **settings):
   config = Configurator()
   config.scan()

我认为containment谓词在这里仍然有效。在

@notfound_view_config(containment=Foo)
def notfound(request):
    return HTTPNotFound('no views for Foo detected for view %s' % request.view_name)

相关问题 更多 >

    热门问题