python tornado.web.uimodules无法使用jinja2进行渲染

时间:2017-10-16 15:42:45

标签: python jinja2 tornado

我在一个简单的网络应用上运行龙卷风。目前,我正在尝试创建一个用户模块,以在base.html上包含的navbar.html组件上显示登录的用户信息。

我创建了一个uimodules.py文件,添加了我的Menu类

import tornado.web

class Menu(tornado.web.UIModule):
    def render(self):
    return '<div>THE MODULE HAS ARRIVED</div>'

然后在我的app.py中导入了这个

from uimodules import Menu

在我的设置中,我有

"ui_modules": {"Menu": Menu}

我的理解是,现在,我的整个应用程序都可以访问“菜单”模块,我应该可以添加...

{% module Menu() %}

...标记包含此模块。当我运行应用程序时,我收到以下错误。

Traceback (most recent call last):
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/tornado/web.py", line 1509, in _execute
    result = method(*self.path_args, **self.path_kwargs)
  File "app.py", line 69, in get
    self.render_template("/pages/index.html", {"login": login, "user": user})
  File "app.py", line 60, in render_template
    self.write(template.render(**context))
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/Users/devmode/Documents/projects/tornado_starter/myapp/templates/components/navbar.html", line 6, in template
    {% module Menu() %}
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/environment.py", line 1005, in render
    return concat(self.root_render_func(self.new_context(vars)))
  File "/Users/devmode/Documents/projects/tornado_starter/myapp/templates/pages/index.html", line 1, in top-level template code
    {% extends 'base.html' %}
  File "/Users/devmode/Documents/projects/tornado_starter/myapp/templates/base.html", line 17, in top-level template code
    {% include 'components/navbar.html' %}
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/Users/devmode/Documents/projects/tornado_starter/myapp/templates/components/navbar.html", line 6, in template
    {% module Menu() %}
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/environment.py", line 497, in _parse
    return Parser(self, source, name, encode_filename(filename)).parse()
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/parser.py", line 896, in parse
    result = nodes.Template(self.subparse(), lineno=1)
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/parser.py", line 878, in subparse
    rv = self.parse_statement()
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/parser.py", line 144, in parse_statement
    self.fail_unknown_tag(token.value, token.lineno)
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/parser.py", line 97, in fail_unknown_tag
    return self._fail_ut_eof(name, self._end_token_stack, lineno)
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/parser.py", line 90, in _fail_ut_eof
    self.fail(' '.join(message), lineno)
  File "/Users/devmode/.virtualenvs/tornado_starter/lib/python3.6/site-packages/jinja2/parser.py", line 59, in fail
    raise exc(msg, lineno, self.name, self.filename)
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'module'.

我已经在github上看过设置了

https://github.com/sunrize531/cherry-forms/blob/master/cherryforms/widgets.py

和龙卷风文件

http://www.tornadoweb.org/en/stable/guide/templates.html

1 个答案:

答案 0 :(得分:0)

Tornado的UIModules是Tornado模板系统的一项功能,不适用于其他模板系统,如jinja2。 (可能有可能为此构建某种适配器,但据我所知,没有人有)。