我以这种方式管理“真正的”404 错误:
application = webapp.WSGIApplication([
('/', MainPage),
#Some others urls
('/.*',Trow404) #I got the 404 page
],debug=False)
但是在我的代码的某些部分我抛出了 404 错误
self.error(404)
我想显示与之前提到的相同的页面,¿有什么方法可以捕获 404 错误并进行管理?
我可以重定向到一些不存在的 url,但看起来很丑
最佳答案
执行此操作的最简单方法是重写基本处理程序(假设您有一个)上的 error() 方法以生成 404 页面,并从常规处理程序和 404 处理程序中调用它。例如:
class BaseHandler(webapp.RequestHandler):
def error(self, code):
super(BaseHandler, self).error(code)
if code == 404:
# Output 404 page
class MyHandler(BaseHandler):
def get(self, some_id):
some_obj = SomeModel.get_by_id(some_id)
if not some_obj:
self.error(404)
return
# ...
class Error404Handler(BaseHandler):
def get(self):
self.error(404)
关于python - 在 appengine 中通过代码处理 404 抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2142198/