【问题标题】:Responding with 404 as status code in Webapp2在 Webapp2 中以 404 作为状态码响应
【发布时间】:2015-11-01 08:15:15
【问题描述】:

我的应用通过读取 URL 来动态生成页面。例如,它将处理所有格式如下的 URL:

[url]/word

如果/word 是一个有效的 URL,那么应用程序将生成一个页面并将其返回。当应用找不到任何有用的东西时,它应该返回一个 404 页面。

我该怎么做?更具体地说,如何将状态码设置为 404?

【问题讨论】:

    标签: python google-app-engine webapp2


    【解决方案1】:

    在您的 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.
      
    • webapp2.abort():

      引发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.
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 2019-04-11
      相关资源
      最近更新 更多