-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

2
  • use {{ l[lv] }} instead of {{ lv }} ? Commented Nov 17, 2012 at 13:32
  • mmm, i don't think i can use [] in templates. anyway i found the problem. i need to create touple with () not {} Commented Nov 17, 2012 at 13:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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'])]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.