How to Iterate Through Dictionary in a Dictionary in Django Template

how to iterate through dictionary in a dictionary in django template?

Lets say your data is -

data = {'a': [ [1, 2] ], 'b': [ [3, 4] ],'c':[ [5,6]] }

You can use the data.items() method to get the dictionary elements. Note, in django templates we do NOT put (). Also some users mentioned values[0] does not work, if that is the case then try values.items.

<table>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>

{% for key, values in data.items %}
<tr>
<td>{{key}}</td>
{% for v in values[0] %}
<td>{{v}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>

Am pretty sure you can extend this logic to your specific dict.


To iterate over dict keys in a sorted order - First we sort in python then iterate & render in django template.

return render_to_response('some_page.html', {'data': sorted(data.items())})

In template file:

{% for key, value in data %}
<tr>
<td> Key: {{ key }} </td>
<td> Value: {{ value }} </td>
</tr>
{% endfor %}

How do I iterate through a dictionary in Django Templates?

{% for key, value in myDict.items %} 

Iterate through non-queryset dictionary in a template

You have a few ways to write this, first read up on the documentation for django template language.

Hopefully you understand that to get the list of days stored as a value against the day key in python you would do something like context['graph_data']['day'].

In the template we use . to access dictionary keys, so we can use graph_data.day in the template to get the same list of days that context['graph_data']['day'] would produce in python.

So I would do it like this:

new Chart(ctx1, {
...
data: {
labels:[{% for i in graph_data.day %}{{ i }},{% endfor %}],
...
}
}

Want to iterate through dictionary with dynamic keys in django template

The only way to use dynamic keys in a Django template is to write a custom filter.

You can create a get_dictionnary_item.py in your templatetags folder :

from django.template.defaulttags import register

@register.filter
def get_dictionary_item(dictionary, key):
return dictionary.get(key)

Then you can use it in your template:

{% load get_dictionary_item %}
...
{{ ledgers|get_dictionary_item:ledger }}

How to iterate through self made dictionary and access its attributes in DJANGO templates.?

CartList is a dictionary, not a list of dictionaries, so you access the items from the dictionary with:

<tr>
<td>{{ CartList.Name }}</td>
<td class="text-center">Rs.{{ CartList.Pricse }}</td>
<td class="text-center">{{ CartList.Quantity }}</td>
<td class="text-right">Rs.{{ CartList.Total_pricse }}</td>
</tr>

That being said, since CartList is a dictionary, and not a list of dictionaries, it might be better to rename it to cart or something similar. Furthermore often the keys are written in snake_case, not PerlCase. There is also a typo for Pricse: it is price, not pricse.

How to iterate through a dictionary with lists in django template?

You should use a nested loop in your case:

{% for cntr, rgn in regions.items %}
{% for r in rgn %}
<option value="{{ r }}">{{ r }}</option>
{% endfor %}
{% endfor %}


Related Topics



Leave a reply



Submit