2👍
✅
You can instead pass the dictionary to your template and access it by iterating over the values using dict.items
in your template.
{% for key1,value1 in json.document.items %}
<div class="section">
<h4><a href="#" class="section_toggle"></a> {{ key1|title }}:</h4>
<table class="table table-condensed">
{% for key2,value2 in value1.items %}
{{ key2 }}
{% endfor %}
</table>
</div>
{% endfor %}
In your code above, it was printing "section1"
letter by letter because you were not iterating over the values of the section1
key but on the section1
string itself. If you need to access the items in a dictionary, you need to use individual variables for keys and values and use dict.items
.
For example, the below code would print the keys and values of the data
dictionary in the template.
{% for key, value in data.items %}
{{ key }}: {{ value }}
{% endfor %}
Source:stackexchange.com