1

我正在使用 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>"
4

2 回答 2

1

正如您已经提到的,static_url是 的一种方法tornado.web.RequestHandler,但您将其称为全局函数。

改变

static_url(...)

self.handler.static_url(...)
于 2012-05-23T10:30:48.110 回答
0

你不需要。如果你给出一个相对路径(在javascript_filesand中css_files,Tornado 会自动运行它通过static_url.

于 2012-05-23T08:07:15.103 回答