2015-11-01 75 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. 
    
相關問題