[Fixed]-Django template and dictionary of lists

17๐Ÿ‘

โœ…

I supose that you are looking for a nested loop. In external loop you do something with dictionary key and, in nested loop, you iterate over iterable dictionary value, a list in your case.

In this case, this is the control flow that you need:

{% for key, value_list  in example_dictionary.items %}
  # stuff here (1)
  {% for value in value_list %}
    # more stuff here (2)
  {% endfor %}
{% endfor %}

A sample:

#view to template ctx:
example_dictionary = {'a' : [1,2]}

#template:
{% for key, value_list  in example_dictionary.items %}
  The key is {{key}}
  {% for value in value_list %}
    The key is {{key}} and the value is {{value}}
  {% endfor %}
{% endfor %}

Results will be:

The key is a
The key is a and the value is 1
The key is a and the value is 2

If this is not that you are looking for, please, use a sample to ilustrate your needs.

๐Ÿ‘คdani herrera

Leave a comment