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

2012-01-11 00:46:55 +08:00
 fanzeyi
找到两种方法:

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

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

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

(正在扒拉Tornado源码的某人..
7291 次点击
所在节点    Tornado
7 条回复
fanzeyi
2012-01-11 00:50:50 +08:00
简单看了下Tornado的代码.. 似乎可以通过重写 tornado.web.HTTPError 来实现?
Livid
2012-01-11 00:51:57 +08:00
override write_error
fanzeyi
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
2012-05-05 22:18:19 +08:00
补充一条,
如果tornado 没有找到匹配的url最后会出现很丑的默认错误页面
所以

http://gist.github.com/2602795
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
2012-05-10 11:24:40 +08:00
习惯是set_status(404),然后render的,是不是不正规呢?
fsw90628
2012-05-10 15:16:42 +08:00
@livid 这个 http://v2ex.appspot.com/ 有解决办法不?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/25358

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX