2014-09-30 89 views
0

我使用鲍尔抢我的网站.css和.js文件。困惑与龙卷风服务器的路径设置

的目录结构如下所示:

server.py bower_components/ 模板/

所有的HTML模板(和我AngularJS用一些谐音模板)位于模板。 bower安装的东西位于bower_components中。我如何定义template_path,static_path和static_url_prefix设置,以便我可以链接到这些目录中的文件?

如果我喜欢使用相对路径:

HREF = “bower_components /引导/距离/ CSS/bootstrap.css”

我会得到404错误。没有处理程序可以找到,所以错误抛出。看来,龙卷风迫使我在我的模板上使用static_url函数?我真的必须使用它吗?当与AngularJS混合使用时,它看起来非常难看。我曾尝试设置static_path到os.path.dirname(文件),并尝试使用static_url但是,让我异常:

例外:您必须定义“static_path”在你的应用程序设置为使用static_url

我应该如何配置?我无法相信我已经浪费了几个小时试图已经想出解决办法... :(

回答

2

你有没有尝试使用StaticFileHandler您可以使用这样的事情:

import os 
import tornado.web 
import tornado.ioloop 

if __name__ == '__main__': 
    app = tornado.web.Application([ 
     (r"/(.*)", tornado.web.StaticFileHandler, {"path": os.path.dirname(os.path.abspath(__file__))}), 
    ]) 
    app.listen(8888) 
    tornado.ioloop.IOLoop.instance().start() 
0

原来,我需要使用tornado.web.StaticFileHandler类映射到路径这是我如何配置我的龙卷风b.Application:

class TornadoApp(tornado.web.Application): 

    def __init__(self): 
     # static file regex patterns matched with the actual folders here 
     static_handlers = { 
      '/bower_components/(.*)': 'bower_components', 
      '/templates/partials/(.*)': 'templates/partials' 
     } 
     handlers = [ 
      ('/', IndexHandler), 
      ('/products', ProductHandler), 
     ] 
     # append the handlers with content from static_handlers dictionary 
     for r, p in static_handlers.items(): 
      handlers.append((r, tornado.web.StaticFileHandler, {'path': p})) 
     settings = { 
      'template_path': "templates", 
      'debug': True 
     } 
     tornado.web.Application.__init__(self, handlers, **settings)