Noo*_*oor 5 python dictionary jinja2
我在 jinja 中遇到了一个奇怪的问题。看起来很简单,但我做对了。在带有{{tag["tag"] }}
它的 jinja 模板中,它正在回显,{u'type': u'literal', u'value': u'tourism'}
但是当我尝试使用 获取值时{{tag["tag"]["value"] }}
,我jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'tag'
从以下 strace 中收到错误:
Traceback (most recent call last):
File "vocabularies.py", line 16, in <module>
table_html = ontology_table.render(fields=["title","domain","tags","expressivity"],rows=table_data["data"])
File "/usr/lib/python2.7/site-packages/Jinja2-2.7.3-py2.7.egg/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python2.7/site-packages/Jinja2-2.7.3-py2.7.egg/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "<template>", line 42, in top-level template code
File "/usr/lib/python2.7/site-packages/Jinja2-2.7.3-py2.7.egg/jinja2/environment.py", line 378, in getitem
return obj[argument]
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'tag'
Run Code Online (Sandbox Code Playgroud)
事实上,我正在加载一个 json 字符串,其中包含一个标签对象,如
{"tags": [{"tagObj": {"type": "uri", "value": "http://ci.emse.fr/opensensingcity/ns/sca/tourism"}, "tag": {"type": "literal", "value": "tourism"}}]}
Run Code Online (Sandbox Code Playgroud)
下面的 jinja 代码因我提供的堆栈跟踪而失败:
{% for tag in row["tags"]%}
<span class="label label-info">{{tag["tag"]["value"] }}</span>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
tag = {"tags": [{"tagObj": {"type": "uri", "value": "http://ci.emse.fr/opensensingcity/ns/sca/tourism"}, "tag": {"type": "literal", "value": "tourism"}}]}
Run Code Online (Sandbox Code Playgroud)
你可以使用它来获得价值tag['tags'][0]['tag']['value']
,你的输出将是'tourism'
这样的。
归档时间: |
|
查看次数: |
5036 次 |
最近记录: |