Tornado Documentation
http://www.v2ex.com/tornado/
Tornado on GitHub
https://github.com/facebook/tornado/
Tornado Gists
http://tornadogists.org/
fanzeyi
V2EX  ›  Tornado

你们是如何自定义HTTP Error页面的?

  •  
  •   fanzeyi · Jan 11, 2012 · 8120 views
    This topic created in 5260 days ago, the information mentioned may be changed or developed.
    找到两种方法:

    1. 重写 tornado.web.ErrorHandler ... 测试了下这种只能 catch 到普通的 404 错误... 更多的比如 raise tornado.web.HTTPError(404) 这样的就处理不了

    2. 就是在人工 raise 错误前... 生成一个错误页面.. 然后用 nginx 之类的转向到这个页面.. 但是一旦同时出现错误... 会导致各种混乱..

    我觉得这两种方法都不是很完美... 有更好的方法么?

    (正在扒拉Tornado源码的某人..
    7 replies    1970-01-01 08:00:00 +08:00
    fanzeyi
        1
    fanzeyi  
    OP
       Jan 11, 2012
    简单看了下Tornado的代码.. 似乎可以通过重写 tornado.web.HTTPError 来实现?
    Livid
        2
    Livid  
    MOD
    PRO
       Jan 11, 2012
    override write_error
    fanzeyi
        3
    fanzeyi  
    OP
       Jan 11, 2012
    @Livid got it.. thanks..

    其实不用重写 write_error ... 直接在 BaseHandler 中提供一个 get_error_html 函数就可以处理了

    get_error_html(self, status_code, **kwargs)

    thanks
    tioover
        4
    tioover  
       May 5, 2012
    补充一条,
    如果tornado 没有找到匹配的url最后会出现很丑的默认错误页面
    所以

    http://gist.github.com/2602795
    Janpo
        5
    Janpo  
       May 10, 2012
    In Tornado 2.0 and earlier, custom error pages were implemented by overriding RequestHandler.get_error_html, which returned the error page as a string instead of calling the normal output methods (and had slightly different semantics for exceptions). This method is still supported, but it is deprecated and applications are encouraged to switch to RequestHandler.write_error
    CoX
        6
    CoX  
       May 10, 2012
    习惯是set_status(404),然后render的,是不是不正规呢?
    fsw90628
        7
    fsw90628  
       May 10, 2012
    @livid 这个 http://v2ex.appspot.com/ 有解决办法不?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1573 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 16:39 · PVG 00:39 · LAX 09:39 · JFK 12:39
    ♥ Do have faith in what you're doing.