【发布时间】:2015-09-28 05:27:07
【问题描述】:
我有以下 JSON 文档:
{
"document": {
"section1": {
"item1": "Item1Value",
"item2": "Item2Value"
},
"section2": {
"item1": "Item1Value",
"item2": "Item2Value"
},
"section3": {
"item1": "Item1Value",
"item2": "Item2Value"
}
}
}
我想在我的 django 模板中显示。该文档将是动态的,因此我想动态显示每个部分。我正在将解析的(通过 json.loads())字符串传递给模板。
我尝试了类似的方法:
{% for section in json.document %}
<div class="section">
<h4><a href="#" class="section_toggle"></a> {{ section|title }}:</h4>
<table class="table table-condensed">
{% for field in json.document.section %}
{{ field }} {# <---- THIS should print item1, item2... Instead, its printing "section1" letter by letter etc #}
{% endfor %}
</table>
</div>
{% endfor %}
但它不能正确打印部分的项目。有什么帮助吗?
【问题讨论】:
-
您的 Json 格式已关闭。
document值需要是您的代码(至少在理论上)工作的列表。 -
这实际上是一个 JSON 字符串,还是一个字典?显示您从哪里获取它以及如何将它传递给视图。
标签: python json django django-templates