Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key Value For Loop Template - Django Inquiry

Having trouble using Key Value For Loops in the Django Template system and was wondering if anyone could help. I am only able to get results if I include at the end of the for loop "values.1.items" instead of "values.items" which produces nothing.

There are many "values" and I can't for the life of me figure out why I have to specify each item numerically. I want to display all the items through this loop. Thanks for the help!

{% for key, values in obj_as_json.items %}
    {% for k, v in values.1.items %}
        {{ k }}: {{ v }}<br><br>
    {% endfor %}
{% endfor %}
like image 322
Jack Avatar asked Feb 11 '14 20:02

Jack


1 Answers

values is a list of of dictionaries rather than a dictionary or a simple list it would look something like this

values = [{'k':'v'},{'k1':'v1'},...]

you can loop over it like a list

{% for key, values in obj_as_json.items %}
    {% for mydict in values %}
        {%for k,v in mydict.items %}
             ....

alternatively you could access it by its index in the list

 {% for k,v in values.1.items %}

is basically the same as

  for k,v in values[1].items():

in normal python

like image 125
Joran Beasley Avatar answered Sep 28 '22 15:09

Joran Beasley