How tornado captures and handles 404 errors

Source: Internet
Author: User

As mentioned in Tornado, only errors in path requests listed in handlers can be captured.

If only one rule (r "/hello", HelloHandler) is defined, only requests with undefined paths such as/hello/other can be captured, the default 404 error page of Tornado is displayed directly, such as/he,/helloworld, And/he/other, without displaying custom error pages.

The solution is simple. You only need to add one (r ". * ", BaseHandler) is used to capture all requests not captured by other rules, overwrite the get method, and call the custom write_error method in the method. Example:

Copy codeThe Code is as follows:
Class BaseHandler (tornado. web. RequestHandler ):
Def get (self ):
Self. write_error (404)

Def write_error (self, status_code, ** kwargs ):
If status_code = 404:
Self. render ('Public/404.html ')
Elif status_code = 500:
Self. render ('Public/500.html ')
Else:
Self. write ('error: '+ str (status_code ))

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.