4๐
โ
You can use a custom filter to accomplish this. If the keys are the same, first define a custom filter as described in this answer:
from django.template.defaulttags import register
...
@register.filter
def get_item(dictionary, key):
return dictionary.get(key)
then you can do the following in your template:
{% for key, value1 in dict1.items %}
<td>Value 1: {{ value1 }}</td>
<td>Value 2: {{ dict2|get_item:key }}</td>
{% endfor %}
Also see this question for another workaround.
๐คSelcuk
0๐
With a little reorganising in your view you can add as many dictionaries to your template as you want and simultaneously loop through by adding them to a list of dictionaries e.g.
dict1 = {'key1':value1, 'key2':value2, 'key3': etc.}
dict2 = {'key1':value1, 'key2':value2, 'key3': etc.}
And you want to loop through both lists in your template simultaneously by creating list object. Assuming above lists come from some database objects:
datas = []
for item in items:
# create new dictionary combining values required
data = {'name':item.name, 'rev':item.rev, 'val':item.val}
datas.append(data)
And then send your list of dictionaries (as you would a filter of django objects) to your template:
{% for data in datas %}
<td>{{ data.name }}</td>
<td>{{ data.value1 }}</td>
<td>{{ data.value2 }}</td>
{% endfor %}
๐คJosh
Source:stackexchange.com