Responding with 404 as status code in Webapp2

时光怂恿深爱的人放手 提交于 2019-12-12 02:27:46

问题


My app is dynamically generating pages by reading the URL. For example, it will handle all URLs formatted like this:

[url]/word

If /word is a valid URL then the app will generate a page and return it back. When the app can't find anything useful it should return a 404 page.

How can I do that? More specifically how do I set the status code to 404?


回答1:


From within your RequestHandler, you can simply call self.abort(404) or webapp2.abort(404) to set the error status code.

References:

  • webapp2.RequestHandler.abort():

    Raises an HTTPException.

    This stops code execution, leaving the HTTP exception to be handled by an exception handler.

    Parameters:

    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():

    Raises an HTTPException.

    Parameters:

    code – An integer that represents a valid HTTP status code.
    args – Positional arguments to instantiate the exception.
    kwargs – Keyword arguments to instantiate the exception.
    


来源:https://stackoverflow.com/questions/33460357/responding-with-404-as-status-code-in-webapp2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!