【发布时间】:2015-11-01 08:15:15
【问题描述】:
我的应用通过读取 URL 来动态生成页面。例如,它将处理所有格式如下的 URL:
[url]/word
如果/word 是一个有效的 URL,那么应用程序将生成一个页面并将其返回。当应用找不到任何有用的东西时,它应该返回一个 404 页面。
我该怎么做?更具体地说,如何将状态码设置为 404?
【问题讨论】:
标签: python google-app-engine webapp2
我的应用通过读取 URL 来动态生成页面。例如,它将处理所有格式如下的 URL:
[url]/word
如果/word 是一个有效的 URL,那么应用程序将生成一个页面并将其返回。当应用找不到任何有用的东西时,它应该返回一个 404 页面。
我该怎么做?更具体地说,如何将状态码设置为 404?
【问题讨论】:
标签: python google-app-engine webapp2
在您的 RequestHandler 中,您可以简单地调用 self.abort(404) 或 webapp2.abort(404) 来设置错误状态代码。
参考资料:
webapp2.RequestHandler.abort():
引发
HTTPException。这会停止代码执行,让 HTTP 异常由 异常处理程序。
参数:
code – HTTP status code (e.g., 404). args – Positional arguments to be passed to the exception class. kwargs – Keyword arguments to be passed to the exception class.
引发
HTTPException。参数:
code – An integer that represents a valid HTTP status code. args – Positional arguments to instantiate the exception. kwargs – Keyword arguments to instantiate the exception.
【讨论】: