简体   繁体   English

在龙卷风中,如何在没有tornado.web.RequestHandler的类中使用static_url()?

[英]In tornado, how can I use static_url() in a class without tornado.web.RequestHandler?

I'm using Tornado and I want to load some static files in the template. 我正在使用Tornado,我想在模板中加载一些静态文件。 Now I use tornado.web.UIModule to load them. 现在,我使用tornado.web.UIModule加载它们。 But I got some errors which said static_url() is not defined. 但是我遇到了一些错误,该错误表示未定义static_url() So I looked up the documentation and found this function is a method of tornado.web.RequestHandler . 因此,我查阅了文档,发现此函数是tornado.web.RequestHandler的方法。 But how can I load the static files like this function in my class below? 但是,如何在下面的类中加载类似于此功能的静态文件?

# _ * _ 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>"

As you already mentioned, static_url is a method of tornado.web.RequestHandler , but you're calling it as a global function. 正如您已经提到的, static_urltornado.web.RequestHandler的方法,但是您将其称为全局函数。

Change 更改

static_url(...)

to

self.handler.static_url(...)

You don't need to. 不用了 If you give a relative path (in javascript_files and css_files , Tornado will automatically run it through static_url . 如果您提供相对路径(在javascript_filescss_files ,Tornado将通过static_url自动运行它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tornado.web.RequestHandler的简单多线程示例 - simple multithreading example with tornado.web.RequestHandler static_url在Tornado中调用 - static_url calling in Tornado python龙卷风使用mako模板,如何使用static_url方法? - python tornado use mako template, how to use static_url method? 在tornado.web.RequestHandler中初始化是否每次都被调用一次请求/ - Does initialize in tornado.web.RequestHandler get called every time for a request/ 我可以在未安装pycurl的Windows上使用tornado web服务器吗? - Can I use tornado web server on windows without pycurl installed? tornado.web.RequestHandler.render_string()如何在不阻塞整个应用程序的情况下加载模板? - How does tornado.web.RequestHandler.render_string() load a template without blocking the whole application? 如何在龙卷风RequestHandler的准备方法中进行URL路径检查? - How to do url path check in prepare method of tornado RequestHandler? Tornado RequestHandler:如何在获取参数之前解码 url 编码的查询 - Tornado RequestHandler: How to decode url encoded query before getting the arguments 我在哪里可以在龙卷风请求处理程序中缓存熊猫数据帧 - Where can i cache pandas dataframe in tornado requesthandler 了解tornado.web.RequestHandler.flush - Understanding tornado.web.RequestHandler.flush
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM