【问题标题】:How to use static file in Tornado如何在 Tornado 中使用静态文件
【发布时间】:2023-10-28 19:08:01
【问题描述】:

Tornado 中如何使用静态路径?

我尝试了以下配置:

settings = {
    'static_path' : 'static'
}

但它不起作用。

例如,我的 HTML 中有这一行:

<link href="/static/bootstrap.css" rel="stylesheet">

当我打开这个网址时:http://localhost/static/bootstrap.css

我看到一个错误:404 Not Found

谁能解释一下如何在 Tornado 中配置静态路径?

【问题讨论】:

  • 你真的不应该使用龙卷风来提供静态内容,这不是它的目的(例如参见 nginx)。如果你真的想要(更简单的开发环境,也许)*.com/questions/10165665/… 有一个很好的例子。

标签: python tornado


【解决方案1】:

假设您从与静态文件夹相同的位置运行文件,您需要将设置 dict 更改为以下内容:

settings = dict(
        static_path=os.path.join(os.path.dirname(__file__), "static")
    )

然后在你的html模板中你需要使用static_url:

<link rel="stylesheet" href="{{ static_url("bootstrap.css") }}" />

【讨论】:

最近更新 更多