我正在使用金字塔(1.5.7) + 遍历并跟随documentation我已经尝试了所有可能的方法来获得“未找到异常视图“正在工作。
from pyramid.view import notfound_view_config,forbidden_view_config, view_config
@notfound_view_config(renderer="error/not_found.jinja2")
def not_found_view(request):
request.response.status = 404
return {}
@forbidden_view_config(renderer="error/forbidden.jinja2")
def forbidden_view(request):
return {}
使用上下文:
from pyramid.view import view_config
from pyramid.httpexceptions import HTTPForbidden, HTTPUnauthorized
@view_config(context=HTTPNotFound, renderer="error/not_found.jinja2")
def not_found_view(request):
request.response.status = 404
return {}
@view_config(context=HTTPForbidden, renderer="error/forbidden.jinja2")
def forbidden_view(request):
return {}
我正在使用Scan模式,但我也尝试在配置中添加自定义功能:
def main(globals, **settings):
config = Configurator()
config.add_notfound_view(notfound)
也没有运气,一直得到以下未处理的例外:
raise HTTPNotFound(msg)
pyramid.httpexceptions.HTTPNotFound: /example-url
答案 0 :(得分:1)
def predispatch_factory(handler, registry):
# one-time configuration code goes here
def predispatch(request):
# code to be executed for each request before
# the actual application code goes here
response = handler(request)
# code to be executed for each request after
# the actual application code goes here
return response
return predispatch
仍然我不知道为什么但是在删除这个补间后,所有似乎都按预期工作。