Jinja2:HOW-TO渲染一个字典| UndefinedError

时间:2015-08-15 09:27:10

标签: python jinja2

我有模板.html存储在变量“email_html”中 我还有一个名为“info”的字典,定义为

info = {'username': u'snip', 'netlink': [u'> F: IL00091 = AMS/VIE/LE-134621 : None : None | O: at-vie01a- : xe-3/1/3 _ et-2/1/21 : nl-ams05a- | W: None : None _ None : None *'], 'end': [2015, 8, 15, 11], 'notes': u'', 'usms': u'ccc', 'start': [2015, 8, 15, 11], 'netelement': [u'> at-vie01a- * x.x.x.x']}

但是当我尝试将info变量加载到模板时:

template = jinja2.Template(open(email_html).read())
html = template.render(**info)

我收到以下错误:

In [13]: html = template.render(**info)
---------------------------------------------------------------------------
UndefinedError                            Traceback (most recent call last)
<ipython-input-13-be5c22c9f3c9> in <module>()
----> 1 html = template.render(**info)

/usr/lib/python2.7/site-packages/jinja2/environment.pyc in render(self, *args, **kwargs)
    967         except Exception:
    968             exc_info = sys.exc_info()
--> 969         return self.environment.handle_exception(exc_info, True)
    970 
    971     def stream(self, *args, **kwargs):

/usr/lib/python2.7/site-packages/jinja2/environment.pyc in handle_exception(self, exc_info, rendered, source_hint)
    740             self.exception_handler(traceback)
    741         exc_type, exc_value, tb = traceback.standard_exc_info
--> 742         reraise(exc_type, exc_value, tb)
    743 
    744     def join_path(self, template, parent):

<template> in top-level template code()

/usr/lib/python2.7/site-packages/jinja2/environment.pyc in getitem(self, obj, argument)
    376         """Get an item or attribute of an object but prefer the item."""
    377         try:
--> 378             return obj[argument]
    379         except (TypeError, LookupError):
    380             if isinstance(argument, string_types):

UndefinedError: 'info' is undefined

这是普通的Python(没有Django,没有Flask)。 你能告诉我做错了吗?

0 个答案:

没有答案