Tornado - 为什么没有 StaticFileHandler 的静态文件不起作用?

Tornado - why does static files without StaticFileHandler donot work?

我是 Tornado 的新手。我正在尝试 link 一个 CSS 文件到 html 模板。我在龙卷风中使用 jinja2。但由于某些未知原因,CSS 文件未加载。

编辑:我已经创建了我的自定义 render_template 函数,它运行良好。

目录结构:

app.py
static
    css
        custom.css
templates
    index.html

这是我的请求处理程序的 Class:

class Index(RequestHandler):
def get(self):
    path = os.path.join('static/css/', 'custom.css')
    return self.write(render_template('index.html', path = path))

这是我的 index.html 模板:

<!Doctype html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="{{path}}"/>
  </head>
  <body>
    <div>
      <div class="header">
         asdasdasd
      </div>
    </div><!--wrapper-->
  </body>

</html>

但浏览器返回 css 文件的 404-NotFound 错误 url,即 <a href="http://localhost/static/css/custom.css" rel="nofollow">http://localhost/static/css/custom.css</a>

这里是关于如何在 tornado 中 link 静态文件的指南: http://tornado.readthedocs.org/en/latest/guide/running.html#static-files-and-aggressive-file-caching

重要的是设置字典中的 'static_path' 设置:

settings = {
    "static_path": os.path.join(os.path.dirname(__file__), "static")
}

现在应该可以使用了。