-1

I'm coming from this question Use variable as dictionary key in Django template

i've this context created in my view:

{'cats': {'LIST1': ['a','b','c'], 'LIST2': ['aa','bb','cc','dd'], 'LIST3': ['f','g']}}

what i want to do is to print the list title and then all the items, something like

LIST1:
- a
- b
- c 

for all the lists, so i did this in my template

{% for  l_name, l in cats %}
    {{ l_name }}

    {%for lv in l %}
        {{ lv }}
    {% endfor %}
{% endfor %}

and this prints only the list names, without printing out the list. where's the mistake?

thanks

Community
  • 1
  • 1
EsseTi
  • 4,079
  • 5
  • 36
  • 63

2 Answers2

5

If you want to iterate over keys and values, you can use:

{% for name, lst in cats.iteritems %}
.... 
{% endfor %}

This just calls the iteritems method of a dictionary, which returns an iterator over a list of 2-tuples.

Django's {% for %} tag documentation has some nice example of this too.

lqc
  • 7,434
  • 1
  • 25
  • 25
0

Just for the record, the problem is how the data are created. so instead of

{'cats': {'LIST1': ['a','b','c'], 'LIST2': ['aa','bb','cc','dd'], 'LIST3': ['f','g']}}

i need a list of touple so:

{'cats': [('LIST1', ['a','b','c']), ('LIST2', ['aa','bb','cc','dd']), ('LIST3', ['f','g'])]}
EsseTi
  • 4,079
  • 5
  • 36
  • 63