[英]confused about how the IOLoop in Tornado picks up the Application object
[英]Confused about path settings with Tornado server
我正在使用Bower來獲取我網站的.css和.js文件。
目錄結構如下所示:
server.py bower_components /模板/
所有HTML模板(以及與AngularJS一起使用的某些局部模板)都位於模板中。 bower安裝的資料位於bower_components中。 如何定義template_path,static_path和static_url_prefix設置,以便可以鏈接到這些目錄中的文件?
如果我使用類似的相對路徑:
href =“ bower_components / bootstrap / dist / css / bootstrap.css”
我會收到404錯誤。 找不到處理程序,因此引發了錯誤。 似乎龍卷風強迫我在模板上使用static_url函數? 我真的必須使用它嗎? 與AngularJS混合使用時,看起來非常難看。 我嘗試將static_path設置為os.path.dirname( file ),並嘗試使用static_url,但這給了我異常:
例外:您必須在應用程序中定義“ static_path”設置才能使用static_url
我應該如何配置呢? 我不敢相信我已經浪費了很多時間試圖找出答案... :(
您是否嘗試過使用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()
您必須將staticfilehandler與放置在settings變量中的一些路徑變量結合使用。 http://tornado.readthedocs.org/en/latest/web.html非常簡單。 您可以根據需要命名任意多個設置變量。
原來,我需要使用tornado.web.StaticFileHandler類,並將其映射到路徑。 這是我配置tornado.web.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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.