你们是如何自定义HTTP Error页面的? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fanzeyi
V2EX    Tornado

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

  •  
  •   fanzeyi 2012-01-11 00:46:55 +08:00 7774 次点击
    这是一个创建于 5022 天前的主题,其中的信息可能已经有所发展或是发生改变。
    找到两种方法:

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

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

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

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

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

    get_error_html(self, status_code, **kwargs)

    thanks
    tioover
        4
    tioover  
       2012-05-05 22:18:19 +08:00
    补充一条,
    如果tornado 没有找到匹配的url最后会出现很丑的默认错误页面
    所以

    http://gist.github.com/2602795
    Janpo
        5
    Janpo  
       2012-05-10 10:53:19 +08:00
    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  
       2012-05-10 11:24:40 +08:00
    习惯是set_status(404),然后render的,是不是不正规呢?
    fsw90628
        7
    fsw90628  
       2012-05-10 15:16:42 +08:00
    @livid 这个 http://v2ex.appspot.com/ 有解决办法不?
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1022 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 20ms UTC 18:33 PVG 02:33 LAX 11:33 JFK 14:33
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86