我在python配置文件中定义了以下字典:
AUTHORS = {
u'MyName Here': {
u'blurb': """ blurb about author""",
u'friendly_name': "Friendly Name",
u'url': 'http://example.com'
}
}
我有以下Jinja2模板:
{% macro article_author(article) %}
{{ article.author }}
{{ AUTHORS }}
{% if article.author %}
<a itemprop="url" href="{{ AUTHORS[article.author]['url'] }}" rel="author"><span itemprop="name">{{ AUTHORS[article.author]['friendly_name'] }}</span></a> -
{{ AUTHORS[article.author]['blurb'] }}
{% endif %}
{% endmacro %}
我称之为:
<div itemprop="author creator" itemscope itemtype="http://schema.org/Person">
{% from '_includes/article_author.html' import article_author with context %}
{{ article_author(article) }}
</div>
生成鹈鹕模板时,出现以下错误:
CRITICAL: UndefinedError: dict object has no element <Author u'MyName Here'>
如果我从模板中删除{% if article.author %}
块,页面将正确生成{{ AUTHORS }}
变量并正确显示。它显然有一个MyName Here
键:
<div itemprop="author creator" itemscope itemtype="http://schema.org/Person">
MyName Here
{u'MyName Here': {u'url': u'http://example.com', u'friendly_name': u'Friendly Name', u'blurb': u' blurb about author'}}
</div>
如何在模板中正确访问MyName Here
元素?你知道吗
article.author
不仅仅是'Your Name'
,它是an ^{或者,为了减少一些样板文件,您可以使用:
只要你的
JINJA_ENVIRONMENT
的extensions
列表中有'jinja2.ext.with_'
。你知道吗注意在Jinja模板中可以使用
dot.notation
而不是index['notation']
。你知道吗相关问题 更多 >
编程相关推荐