tornado+bootstrap服务器,无法正确识别路径,莫名其妙的设置问题!

qq_33992949 2016-02-16 06:42:37
通过 tornado 的 settings 手动设置 和 RequestHandler 的参数设置 结果居然不一样
1,login.html 代码
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link rel="stylesheet" href="{{ static_url("css/bootstrap.min.css") }}">


<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="http://cdn.bootcss.com/html5shiv/3.7.0/html5shiv.min.js"></script>
<script src="http://cdn.bootcss.com/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>

<div class="container">
<form class="form-signin" role="form">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="form-control" placeholder="Email address" required autofocus>
<input type="password" class="form-control" placeholder="Password" required>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div> <!-- /container -->

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="{{ static_url("js/jquery-2.2.0.min.js") }}"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="{{ static_url("js/bootstrap.min.js") }}"></script>

</body>
</html>


2,服务器端代码
import os.path

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from tornado.options import define, options
define("port", default=8010, help="run on the given port", type=int)

class LoginHandler(tornado.web.RequestHandler):
def get(self):
self.render('login.html')
class ShowHandler(tornado.web.RequestHandler):
def get(self):
self.render('login.html')


if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application([(r"/login",LoginHandler),
(r"/test",ShowHandler)],
template_path = os.path.join(os.path.dirname(__file__),"templates"),
static_path =os.path.join(os.path.dirname(__file__), "static"),
debug = True
)
app1 = tornado.web.Application([(r"/login",LoginHandler),
(r"/test",ShowHandler)],
template_path = os.path.join(os.path.dirname(__file__),"templates"),
debug = True
)
app1.settings['static_path']=os.path.join(os.path.dirname(__file__), "static")
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()


大家注意看这一段代码:
    app = tornado.web.Application([(r"/login",LoginHandler),
(r"/test",ShowHandler)],
template_path = os.path.join(os.path.dirname(__file__),"templates"),
static_path =os.path.join(os.path.dirname(__file__), "static"),
debug = True
)
app1 = tornado.web.Application([(r"/login",LoginHandler),
(r"/test",ShowHandler)],
template_path = os.path.join(os.path.dirname(__file__),"templates"),
debug = True
)
app1.settings['static_path']=os.path.join(os.path.dirname(__file__), "static")
http_server = tornado.httpserver.HTTPServer(app)


为了做对比 我实例化了2个 app
其中app 是最原始的方法。
app1 中 手动制定了 settings 的其中一个属性“static_path”
加断点对比
>>> app.settings == app1.settings
True


说明两个是一样的!!!

但是 当用第一个 app 运行的时候 可以成功加载 css js 和bootstrap
成功!
[I 160216 19:24:47 web:1908] 304 GET /test (127.0.0.1) 23.42ms
[I 160216 19:24:47 web:1908] 304 GET /static/css/bootstrap.min.css?v=2f624089c65f12185e79925bc5a7fc42 (127.0.0.1) 10.48ms
[I 160216 19:24:47 web:1908] 304 GET /static/js/jquery-2.2.0.min.js?v=6fc159d00dc3cea4153c038739683f93 (127.0.0.1) 5.20ms


但是 当用第二个 app1 运行的时候 无法成功加载 css 和bootstrap
不成功!!
[I 160216 19:25:52 web:1908] 304 GET /test (127.0.0.1) 22.28ms
[W 160216 19:25:52 web:1908] 404 GET /static/css/bootstrap.min.css?v=2f624089c65f12185e79925bc5a7fc42 (127.0.0.1) 8.89ms
[W 160216 19:25:52 web:1908] 404 GET /static/js/jquery-2.2.0.min.js?v=6fc159d00dc3cea4153c038739683f93 (127.0.0.1) 4.47ms
[W 160216 19:25:52 web:1908] 404 GET /static/js/bootstrap.min.js?v=c5b5b2fa19bd66ff23211d9f844e0131 (127.0.0.1) 4.85ms


谁能告诉我为什么? 我已经快疯了!!大家谁能告诉我呢?………………
...全文
665 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_35184956 2016-06-01
  • 打赏
  • 举报
回复
我也遇到这个问题,不过已经搞定了; tornado中设置setting; "static_path": os.path.join(os.path.dirname(__file__), "static"), "template_path":os.path.join(os.path.dirname(__file__), "templates") 设置静态文件的路径为当前路径加statis(只能设置为static) 模板文件(使用self.render时候),默认是去templates下找文件 前端请求静态文件时候以/static/XX开头 需要tornado渲染的请求/templates/xx开头,然后添加一个路由(r'/templates/.*',mainhandler)这样;
panghuhu250 2016-02-17
  • 打赏
  • 举报
回复
Application(static_path="/static")在Application的__init__中定义了handlers来处理"static file", 而app.setting['static_path']没做这一步, 自然不能处理static file. 下面是Application.__init__的相关部分:

        if self.settings.get("static_path"):
            path = self.settings["static_path"]
            handlers = list(handlers or [])
            static_url_prefix = settings.get("static_url_prefix",
                                             "/static/")
            static_handler_class = settings.get("static_handler_class",
                                                StaticFileHandler)
            static_handler_args = settings.get("static_handler_args", {})
            static_handler_args['path'] = path
            for pattern in [re.escape(static_url_prefix) + r"(.*)",
                            r"/(favicon\.ico)", r"/(robots\.txt)"]:
                handlers.insert(0, (pattern, static_handler_class,
                                    static_handler_args))

37,737

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧