静态文件的 Tornado 自定义错误处理程序 [英] Tornado custom error handler for Static file
问题描述
如何为静态文件显示自定义 404 错误页面?
在我当前的应用程序处理程序中,我添加了如下最后一个模式
<预><代码>[(r'/(favicon.ico)', tornado.web.StaticFileHandler, {"path": mypath}),(r'/foo',FooHandler),(r'/bar',BarHandler),(r'/(.*)',ErrorHandler),]这是我的错误处理程序
class ErrorHandler(BaseHandler):def get(self,d):self.status_code = 404self.show404(d)
如果我访问 http://localhost/abc
我会得到我的自定义 404 页面
但是如果我尝试获取 http://localhost/static/abc.js
我会收到如下丑陋的错误
第 2286 行,在 validate_absolute_path 中引发 HTTPError(404)HTTPError:HTTP 404:未找到
有什么办法可以让它工作吗?如何显示静态文件的自定义错误页面
这有点棘手;您需要继承 StaticFileHandler 并覆盖其 write_error 方法,然后使用 static_handler_class Application 设置安装该类.
How can I show custom 404 error page for static files?
in my current application handler I have added last pattern as follows
[
(r'/(favicon.ico)', tornado.web.StaticFileHandler, {"path": mypath}),
(r'/foo',FooHandler),
(r'/bar',BarHandler),
(r'/(.*)',ErrorHandler),
]
and this is my error handler
class ErrorHandler(BaseHandler):
def get(self,d):
self.status_code = 404
self.show404(d)
If I visit http://localhost/abc
I am getting my custom 404 page
but if I try to get http://localhost/static/abc.js
I'm getting the ugly error like below
line 2286, in validate_absolute_path
raise HTTPError(404)
HTTPError: HTTP 404: Not Found
Is there any way to get this working? How can I show my custom error page for static files
This is a bit tricky; you need to subclass StaticFileHandler and override its write_error method, then install that class with static_handler_class Application setting.
这篇关于静态文件的 Tornado 自定义错误处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!