2015-11-01 117 views
0

我的应用程序通过读取URL动态生成页面。例如,它会处理格式化这样所有的URL:使用404作为Webapp2中的状态代码进行响应

[url]/word 

如果/word是有效的URL,然后该应用程序会生成一个页面,然后返回。当应用程序找不到有用的东西时,它应该返回一个404页面。

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

回答

2

从您的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.