Reputation: 1
I have a list of dictionaries called data.
for every_archive in data I want to acces every_archive['key1']['key2']
First key it's a constant: "units" but the second key depends on a loop.
I have already tried : {{ archive['units'][{{item['param']}}] }}
where item['param']
item is another iterator in a loop and item['param'] is the second key.
Upvotes: 0
Views: 538
Reputation: 184
As far as I understood, you have a list that contains some nested dictionaries; if that's the case, and your data looks like that:
data = [{'foo': {'bar1': 'buzz1', 'bar2': 'buzz2'}}, {'foo': {'bar3': 'buzz3', 'bar4': 'buzz4'}}]
if you use this Jinja2:
{% for every_archive in data %}
{% for key1, archive1 in every_archive.items() %}
{% for key2, value2 in archive1.items() %}
<p>{{ key1 }} - {{ key2 }} - {{ value2 }}</p>
{% endfor %}
{% endfor %}
{% endfor %}
you will get this output:
foo - bar1 - buzz1
foo - bar2 - buzz2
foo - bar3 - buzz3
foo - bar4 - buzz4
also, the same output you will get from
<p>{{ key1 }} - {{ key2 }} - {{ archive1[key2] }}</p>
Upvotes: 0
Reputation: 375
See the format below! The structure is going to be very similar to how you would loop through a dictionary in Python, but with the jinja {% %} for each statement you do not want to display and {{ }} around each expression you want to display.
Taken from How to iterate through a list of dictionaries in jinja template?
Data: parent_dict = [{'A':{'a':1}},{'B':{'b':2}},{'C':{'c':3}},{'D':{'d':4}}]
In Jinja2 iteration:
{% for dict_item in parent_dict %}
{% for key1 in dict_item %}
{% for key2 in dict_item[key1] %}
<h2>Value: {{dict_item[key1][key2]}}</h2>
{% endfor %}
{% endfor %}
{% endfor %}
Upvotes: 1