我正在尝试这个:
favicon_path = '/path/to/favicon.ico'
settings = {'debug': True,
'static_path': os.path.join(PATH, 'static')}
handlers = [(r'/', WebHandler),
(r'/favicon.ico', tornado.web.StaticFileHandler, {'path': favicon_path})]
application = tornado.web.Application(handlers, **settings)
application.listen(port)
tornado.ioloop.IOLoop.instance().start()
但是它一直在我的static_path中提供favicon.ico
(我在两个不同的路径中有两个不同的favicon.ico
,如上所示,但我希望能够覆盖一个在static_path
)。
答案 0 :(得分:50)
从应用设置中删除static_path
。
然后将处理程序设置为:
handlers = [
(r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': favicon_path_dir}),
(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': static_path_dir}),
(r'/', WebHandler)
]
答案 1 :(得分:6)
您需要用括号括起favicon.ico并在正则表达式中转义句点。您的代码将成为
favicon_path = '/path/to/favicon.ico' # Actually the directory containing the favicon.ico file
settings = {
'debug': True,
'static_path': os.path.join(PATH, 'static')}
handlers = [
(r'/', WebHandler),
(r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': favicon_path})]
application = tornado.web.Application(handlers, **settings)
application.listen(port)
tornado.ioloop.IOLoop.instance().start()
答案 2 :(得分:0)
例如
settings = dict(
static_path=os.path.join(os.path.dirname(__file__), 'static'),
static_url_prefix="/adtrpt/static/",
)
将自定义处理程序添加到处理程序
handlers.append((r"/adtrpt/static/(.*)", MyStaticFileHandler, {"path": os.path.join(os.path.dirname(__file__), 'static')}))
然后实现您的自定义方法。
class StaticHandler(BaseHandler):
def get(self):
path = self.request.path
print(path)
self.redirect(BASE_URI + path)