客製化 Tornado 的 StaticFile 的 404 not found page.

Posted in :

滿簡單的,多包一層就可以處理掉。

The file specified in default_filename should be in given static path. So if you move error.htmlto assets/js directory, than navigate to /js/ you will see content of error.html.

Basically this functionality is a helper with limited usecase (imho). More at https://stackoverflow.com/a/27891339/681044.

Custom error pages

Every request handler handles/renders errors in write_error function. This is the recommended way to create custom error pages:

class MyStaticFileHandler(tornado.web.StaticFileHandler):

    def write_error(self, status_code, *args, **kwargs):
        # custom 404 page
        if status_code in [404]:
            self.render('templates/error.html')
        else:
            super().write_error(status_code, *args, **kwargs)

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *