[英]In tornado, how can I use static_url() in a class without tornado.web.RequestHandler?
我正在使用Tornado,我想在模板中加载一些静态文件。 现在,我使用tornado.web.UIModule
加载它们。 但是我遇到了一些错误,该错误表示未定义static_url()
。 因此,我查阅了文档,发现此函数是tornado.web.RequestHandler
的方法。 但是,如何在下面的类中加载类似于此功能的静态文件?
# _ * _ coding:utf-8 _ * _
import tornado.web
from tornado import template
class Header(tornado.web.UIModule):
"""docstring for Header"""
def render(self, hightlight = "index"):
return self.render_string("header.html", hightlight = hightlight)
def css_files(self):
css = [
static_url("css/smoothness/jquery-ui-1.8.20.custom.css"),
static_url("css/common.css"),
static_url("css/jquery.jqplot.min.css"),
static_url("css/blue/style.css"),
static_url("css/jquery.vector-map.css")
]
return css;
def javascript_files(self):
javascript = [
static_url("js/convert.color.js"),
static_url("js/jquery-1.7.2.min.js"),
static_url("js/jquery-ui-1.8.20.custom.min.js"),
static_url("js/common.js"),
static_url("js/jquery.jqplot.min.js"),
static_url("js/plugins/jqplot.highlighter.min.js"),
static_url("js/plugins/jqplot.cursor.min.js"),
static_url("js/plugins/jqplot.dateAxisRenderer.min.js"),
static_url("js/plugins/jqplot.canvasTextRenderer.min.js"),
static_url("js/plugins/jqplot.canvasAxisLabelRenderer.min.js"),
static_url("js/jquery.vector-map.js"),
static_url("js/china-cn.js"),
static_url("js/jquery.tablesorter.min.js"),
static_url("js/charts.js")
]
return javascript
def html_body(self):
return "<!--[if lt IE 9]><script src=\"{{ static_url(\"js/excanvas.js\") }}\"></script><![endif]-->"
def embedded_javascript(self):
return "<script>var current = null;</script>"
正如您已经提到的, static_url
是tornado.web.RequestHandler
的方法,但是您将其称为全局函数。
更改
static_url(...)
至
self.handler.static_url(...)
不用了 如果您提供相对路径(在javascript_files
和css_files
,Tornado将通过static_url
自动运行它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.