我将在 Tornado 中提供静态文件。以下是我的项目的路径:
项目/
/app/app.py
/static/css/<css files>
/static/html/ <html files>
应用程序.py:
def make_app():
settings = {"static_path": os.path.join(os.path.dirname(__file__), "../static")}
return tornado.web.Application([
# (r"/", MainHandler),
(r"/user/authenticate", AuthenticateHandler),
(r"/user/getToken", TokenHandler),
(r"/login", LoginPageHandler),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": html_root, "default_filename": "index.html"}),
(r"/(.css)", tornado.web.StaticFileHandler, dict(path=settings['static_path']))
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
每当我寻找http://localhost:8888/somefile.min.css时它返回 404
最佳答案
(r"/(.*)", tornado.web.StaticFileHandler, {"path": html_root, "default_filename": "index.html"}),
此正则表达式将匹配所有内容,包括您的 css 文件。因此,交换最后两个条目,它应该可以工作。包罗万象的正则表达式通常最好放在最后,因为包罗万象的正则表达式应该如此。
关于python - Tornado不显示静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53151524/