gpt4 book ai didi

Jinja 中的 Python 字典

转载 作者:行者123 更新时间:2023-11-28 16:29:08 24 4
gpt4 key购买 nike

我在 jinja 遇到了一个奇怪的问题。这看起来很简单,但我做对了。在带有 {{tag["tag"] }} 的神社模板中,它正在回显 {u'type': u'literal', u'value': u'tourism'} 但当我尝试使用 {{tag["tag"]["value"] }} 获取值时,出现错误 jinja2.exceptions.UndefinedError: 'dict object' 没有来自以下 strace 的属性 'tag':

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'

事实上,我正在加载一个包含标签对象的 json 字符串,例如

{"tags": [{"tagObj": {"type": "uri", "value": "http://ci.emse.fr/opensensingcity/ns/sca/tourism"}, "tag": {"type": "literal", "value": "tourism"}}]}

下面的 jinja 代码因我提供的堆栈跟踪而失败:

{% for tag in row["tags"]%}
<span class="label label-info">{{tag["tag"]["value"] }}</span>
{% endfor %}

最佳答案

tag = {"tags": [{"tagObj": {"type": "uri", "value": "http://ci.emse.fr/opensensingcity/ns/sca/tourism"}, "tag": {"type": "literal", "value": "tourism"}}]}

您可以使用 tag['tags'][0]['tag']['value'] 获取值这样你的输出将是 'tourism'

关于Jinja 中的 Python 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33824221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com