在404错误时重定向到页面

2 投票
1 回答
1256 浏览
提问于 2025-04-16 21:57

有没有办法在出现404(找不到页面)和500(服务器错误)的时候,直接跳转到一个指定的页面,而不是自己构造一个错误响应呢?我试过这个-

class NotFoundPageHandler(webapp.RequestHandler):
    def get(self):        
        #if 400
        self.redirect('error/notfound.html')
        #if 500 how to check

def main():
    application = webapp.WSGIApplication(
                                       [
                                         ('/', MainPage),
                                         ('/index.html', MainPage),
                                         ('.*', NotFoundPageHandler)
                                       ], debug=True)

但是没成功。

1 个回答

1

你其实不想进行重定向。你想要的是一个自定义的错误页面。

http://code.google.com/appengine/docs/python/config/appconfig.html#Custom_Error_Responses

error_handlers:
  - file: default_error.html

  - error_code: over_quota
    file: over_quota.html

- error_code: 404 或者 - error_code: 500 也应该可以用。仔细阅读那个链接,听起来你需要注意这些文件不能放在静态文件目录里。

撰写回答