pyramid.httpexceptions.HTTPNotFound:资源找不到
问题描述:
我工作过Safari的金字塔教程pyramid.httpexceptions.HTTPNotFound:资源找不到
Python和金字塔框架
WEB应用我views.py
文件里有我下面的代码有问题:
@property
def current(self):
todo_id = self.request.matchdict.get('id')
todo = sample_todos.get(todo_id)
if not todo:
raise HTTPNotFound()
return todo
特别是当下面的视图函数调用这个属性时
@view_config(route_name='view', renderer='templates/view.jinja2')
def view(self):
return dict(todo=self.current)
当我运行的应用程序
http://0.0.0.0:6543/5
不会触发预期
HTTPNotFound()
,请参见下面的路线。
config.add_route('view', '/{id}')
错误日志返回:
File "/Users/alex/zdev/t-oreilly/mysite/views.py", line 50, in view
return dict(todo=self.current)
File "/Users/alex/zdev/t-oreilly/mysite/views.py", line 25, in current
raise HTTPNotFound()
pyramid.httpexceptions.HTTPNotFound: The resource could not be found.
在浏览器女服务员返回默认的服务器错误。
什么是删除此错误的正确方法?
我已上载该作品github,提交aaf562e
教程链接here,对于那些渴望帮助,它可以与他们10天审判进行访问。这个问题是从视频17/48。
谢谢,如果您需要更多信息,请让我知道。
答
在两个Jinja模板中,您引用了@property
view.current
。但是,由于属性抛出异常,因此您的Jinja模板最终会触及并爆炸,从而导致您的问题。
从Jinja模板中删除对view.current
的呼叫或修改您的view.current
函数,使其不会丢失。
我不确定这是否是您正在寻找的解决方案,但它不会偏离本教程。
答
这是一个不同的HTTPNotFound
异常,它会在执行视图前在路由匹配步骤中引发。原因是你有config.add_route('view', '/{id}')
。请注意0不是/{id}/
。金字塔认为这两条不同的路线,因此后者不匹配。最简单的解决方法是将所有规范路线注册为/
后缀,如/{id}/
,然后将append_slash=True
传递给您未发现的视图配置,如config.add_notfound_view(..., append_slash=True)
或@notfound_view_config(append_slash=True)
。这将在用户访问版本时触发重定向,而不使用结尾斜杠。
感谢您的回复。不幸的是,这并不能解决我所面临的主要问题。我添加了更多的错误日志,以便查看问题来自哪里。我很抱歉不尽快这样做。 这个问题,我相信,是行**提高HTTPNotFound()**被称为 –