How to set static path in Tornado?

2019-04-14 11:16发布

I want to set my http server's static directory, and put some pictures in it, so that users can get my pictures use the url. But I failed, code below does not work:

# 静态路径
STATIC_DIRNAME = "resources"
# 设置static path
settings = {
    "static_path": os.path.join(os.path.dirname(__file__), STATIC_DIRNAME),
}

# and I passed settings to Application
app = tornado.web.Application([
    (r"/pictures", handler.PicturesHandler),
], **settings)

How can I set the static directory to "resources"?

(I want to get my picture through url such as: localhost:8888/resources/1.jpg)

1条回答
做个烂人
2楼-- · 2019-04-14 12:04

Use static_url_prefix

settings = {
    "static_path": os.path.join(os.path.dirname(__file__), STATIC_DIRNAME),
    "static_url_prefix": "/resources/",
}
查看更多
登录 后发表回答