Use a custom error page in python / tornado

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

Use a custom error page in python / tornado
Use print in a Python2 lambda expression
Use dates in Python
Use Valgrind in Python
Create a new page in confluence with Python
Custom sort in Python3
Use profiler in Python
If you encounter a "Unicode Decode Error" in Python
Let's use def in python
Take a screenshot in Python
Use let expression in Python
Use Measurement Protocol in Python
Create a function in Python
Create a dictionary in Python
Use callback function in Python
Use parameter store in Python
[Python] Use a string sequence
Use HTTP cache in Python
Use MongoDB ODM in Python
Use list-keyed dict in Python
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE
Make a bookmarklet in Python
Slice error in python (´ ; ω ; `)
Draw a heart in Python
How to use the __call__ method in a Python class
[Django3] Display a web page in Django3 + WSL + Python virtual environment
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
Use fabric as is in python (fabric3)
Create a DI Container in Python
Draw a scatterplot matrix in python
Write A * (A-star) algorithm in Python
Create a binary file in Python
Use rospy with virtualenv in Python3
Use blender as a python module
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
How to use Mysql in python
Use Python in pyenv with NeoVim
Write a depth-first search in Python
How to use ChemSpider in Python
How to use PubChem in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Use OpenCV with Python 3 in Window
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Draw a CNN diagram in Python
Create a random string in Python
Schedule a Zoom meeting in Python
When writing a program in Python
Use a custom kernel with WSL2
Custom state space model in Python
IQ Bot Custom Logic (Python): Efficient replacement process in a loop
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
IQ Bot Custom Logic (Python): Efficient replacement process in a loop