1

I'm using Tornado and I want to load some static files in the template. Now I use tornado.web.UIModule to load them. But I got some errors which said static_url() is not defined. So I looked up the documentation and found this function is a method of 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>"
Melkor
  • 532
  • 8
  • 25

2 Answers2

1

As you already mentioned, static_url is a method of tornado.web.RequestHandler, but you're calling it as a global function.

Change

static_url(...)

to

self.handler.static_url(...)
vartec
  • 131,205
  • 36
  • 218
  • 244
  • no, i've read the documentation that the `static_url()` is a method of tornado.web.RequestHandler, but here i use the `tornado.web.UIModule` instead of `tornado.web.RequestHandler`. Therefore, your suggestion raised the `AttributeError` because the objects here(Header for instance) has no attribute `static_url` – Melkor May 23 '12 at 11:45
  • thanks, this way works, but as @cole-maclean mentioned, tornado will automatically load these static files through `static_url()` – Melkor May 25 '12 at 07:09
0

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.

Cole Maclean
  • 5,627
  • 25
  • 37
  • got it, thank you. and, what is the most common and recommended way to load template? using `tornado.web.UIModule` or just `block` `extends`? – Melkor May 23 '12 at 10:06
  • UIModules are different than templates. I guess using block and extends is simplest. – Cole Maclean May 25 '12 at 01:03
  • are you sure? because I just checked it and tornado doesn't seem to serve via static_url module. If I am not wrong, if it is passed via static_url module there will be version hash in the url (which can be seen in the source code in browser) something like `/static/js/myjs.js?v=058b0f38e33db6f1001750ba9fd93920`. But tornado doesn't seem to do that. But when I import static module in my UI Modules and use it, I see the version hash. – avi Jan 09 '14 at 15:51
  • In required files for UIModules, it's automatic (again, if it's a relative path). In templates, it is not, and static_url must be called. Make sense? – Cole Maclean Jan 09 '14 at 19:37