I got a little addicted when dealing with custom error pages in python / tornado, so I summarized it.
# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
# self.write_Override error
def write_error(self, status_code, exc_info=None, **kwargs):
self.set_header('Content-Type', 'text/html; charset="utf-8"') #Content as appropriate-Declare type(optional)
if status_code == 503:
#You can write it directly
self.finish('<h1>503 Service Temporarily Unavailable</h1>')
elif status_code == 404:
#You may prepare a template separately
self.render('/path/to/templates_dir/404.html',
#Pass the variable to be embedded in the template as a keyword argument
message = 'hogehoge'
)
#Omission...
def get(self):
#Omission...
application = tornado.web.Application([
#Omission...
])
if __name__ == "__main__":
application.listen(port=8080)
tornado.ioloop.IOLoop.instance().start()
Summary
--In the overridden write_error, use self.finish () --self.render () calls self.finish () internally, so no problem
Recommended Posts