我写了一个页面,引用了bootstrap中的文件,刚开始我的目录结构是这样的:
bootstrap
- css
- bootstrap.css
- js
- bootstrap.js
- img
- css
tornado
- example.html
目录中的文件并没有全部列出来。
example.html中引用bootstrap的代码如下:
<link href="../bootstrap/css/bootstrap.css" rel="stylesheet" media="screen">
<script src="http://code.jquery.com/jquery.js"></script>
<script src="../bootstrap/js/bootstrap.min.js"></script>
我的hello.py代码如下:
import tornado.ioloop
import tornado.web
import os
class MainHandler(tornado.web.RequestHandler):
def get(self):
print "here"
self.render("example.html")
application = tornado.web.Application(
[ (r"/", MainHandler) ]
)
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
运行的时候发现bootstrap的样式表和js效果全部没有,在firebug下面看到很多下面这样的错误:
"NetworkError: 404 Not Found - http://localhost:8888/bootstrap/js/bootstrap.js"
然后,我看了tornado的文档,做了如下改动: 在tornado目录下面新建一个static目录,把bootstrap放进去,目录结构如下:
- tornado
- static
- bootstrap
- css
- js
- img
- bootstrap
- hello.py
- example.html
- static
把example.html对bootstrap文件的引用改成下面这样:
<link href="static/bootstrap/css/bootstrap.css" rel="stylesheet" media="screen">
<script src="http://code.jquery.com/jquery.js"></script>
<script src="static/bootstrap/js/bootstrap.min.js"></script>
hello.py修改部分如下:
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static")
}#配置静态文件路径
application = tornado.web.Application(
[(r"/", MainHandler),],**settings
)
再次执行发现结果正常。我很纳闷,tornado一定要配置static_path吗?我之前的错误是在哪里? 而且我把bootstrap目录放回最初的目录,然后这样配置settings:
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "../bootstrap")
}
example.html还是最初那样,这样修改还是错误,求教到底是哪里的问题?
刚也碰到这个问题,正确答案在官方文档的两句话里边:
1、You can serve static files by sending the static_path setting as a keyword argument. We will serve those files from the /static/ URI (this is configurable with the static_url_prefix setting), and we will serve /favicon.ico and /robots.txt from the same directory.
2、static_path: Directory from which static files will be served.
static_url_prefix: Url prefix for static files, defaults to "/static/".
链接:http://tornado.readthedocs.org/en/latest/web.html
如果是tornado处理全部请求的话的确要求写static_path,或者使用tornado.web.StaticHandler,而一般生产环境下部署的时候,我们一般会在前端部署一个nginx的,可以在这里做静态文件处理,把static目录下的文件让nginx来执行。
server
{
listen 80;
server_name www.abc.com;
index index.html index.htm index.php;
root /storage/www/abc;
server_name_in_redirect off;
access_log off;
# Allow file uploads
client_max_body_size 10M;
proxy_read_timeout 10;
location = /favicon.ico {
rewrite (.*) /static/favicon.ico;
}
location = /robots.txt {
rewrite (.*) /static/robots.txt;
}
location ^~ /static/ {
root /storage/www/abc;
if ($query_string) {
expires max;
}
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://abc;
}
}
当然,开发时为了方便,在tornado里配置一个static目录比较好。
要么你配置 static_path
,要么你自己处理静态文件。Tornado 不是 RoR,也不是 Django,不会隐式地帮你 serve 静态文件。
static_path
的挂载路径是 /static
,因此你把文件放回去之后,访问路径应该是 /static/css/bootstrap.css
。
遇到同样的问题 settings = { #"staticpath": os.path.join(os.path.dirname(file), "static"), "staticpath": os.path.join(os.path.dirname(file), "bootstrap"), }
static_path这个变量ms设置不起作用 始终是static
我也遇到了一样的问题,必须设置目录名为static多一个s都不行,否则系统就是找不到路径。