0
我已經在Python配置文件定義了以下解釋:如何訪問Jina2模板中的特定字典元素?
AUTHORS = {
u'MyName Here': {
u'blurb': """ blurb about author""",
u'friendly_name': "Friendly Name",
u'url': 'http://example.com'
}
}
我有以下的Jinja2模板:
<div itemprop="author creator" itemscope itemtype="http://schema.org/Person">
{% from '_includes/article_author.html' import article_author with context %}
{{ article_author(article) }}
</div>
:
{% 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 %}
而且我通過調用這個
當我生成我的Pelican模板時,出現以下錯誤:
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'確實是string/unicode類型的嗎? – Feodoran