我使用的是金字塔(1.5.7) +遍历,并且遵循文档,我已经尝试了所有可能的方法来使"Not异常视图“工作。
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 {}我正在使用扫瞄模式,但我也尝试在配置中添加一个自定义函数:
def main(globals, **settings):
config = Configurator()
config.add_notfound_view(notfound)也不是运气,而是始终得到以下未处理的异常:
raise HTTPNotFound(msg)
pyramid.httpexceptions.HTTPNotFound: /example-url发布于 2015-05-15 13:21:23
哎哟。我的错!我用的是一种防止金字塔的东西来装载例外情况:
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不过,我不知道为什么,但在去除这两者之后,一切似乎都如期而至。
https://stackoverflow.com/questions/30259152
复制相似问题