python - 在tornado中,如何在没有tornado.web.RequestHandler的类中使用static_url()?

标签 python tornado static-linking 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_urltornado.web.RequestHandler 的一种方法,但您将其作为全局函数调用。

改变

static_url(...)

self.handler.static_url(...)

关于python - 在tornado中,如何在没有tornado.web.RequestHandler的类中使用static_url()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10696033/

相关文章:

c++ - 静态链接 pion 网络库

python 3 : sum (union) of dictionaries with "+" operand raises exception

python - 如何在图像时间序列中一致地编号轮廓?

python - 使用BeautifulSoup根据属性检索信息

python - 如何将数字添加到基于另一列的列? (字典)

python - 用户认证 : prepare vs get_current_user in tornado

python - @gen.coroutine 未在带有 Tornado 的 python 中定义

python - Pylint 和 Tornado - 在 @tornado.web.authenticated 上失败

具有构造函数属性的 gcc 函数没有被链接

c - 如何在 C 中创建模块