How do I return HTTP error code without default template in Tornado?

后端 未结 7 2152
情深已故
情深已故 2021-02-01 02:40

I am currently using the following to raise a HTTP bad request:

raise tornado.web.HTTPError(400)

which returns a html output:

&         


        
7条回答
  •  清歌不尽
    2021-02-01 03:19

    def write_error(self, status_code, **kwargs):
        #Function to display custom error page defined in the handler.
        #Over written from base handler.
        data = {}
        data['code'] = status_code
        data['message'] = httplib.responses[status_code]
        # your other conditions here to create data dict
        self.write(TEMPLATES.load('error.html').generate(data=data))
    

    when ever self.send_error() call is initiated write_error() function is called by the request handler. So you can create your custom error data dict here and render it to your custom error page.

    http.responses[status_code] returns the error code text like "page not found" based on the status code.

提交回复
热议问题