Aniket Maithani
Aniket Maithani

Reputation: 865

Iterating through json list in Django template

I am passing the following context in my Django template :

context = {'test': custom_json_list}

And the output of custom_json_list is this :

{'pc_16530587071502': [{'people_count_entry__sum': None}],
 'pc_17100675958928': [{'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': 4},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None},
                       {'people_count_entry__sum': None}]} 

I want to display the data in the following format:

'pc_16530587071502' : NONE
'pc_17100675958928' : None
'pc_17100675958928' : None
'pc_17100675958928' : None
'pc_17100675958928' : None
'pc_17100675958928' : None
'pc_17100675958928' : None
'pc_17100675958928' : 4
'pc_17100675958928' : None
'pc_17100675958928' : None
'pc_17100675958928' : None

How can I proceed with the syntax so that I can see the data in this format.

The only thing I was able to decipher is this :

{% for key, value in test.items %}
     {{ key }} <br />
     {{ value }} <br />
{% endfor %}

Thanks in advance.

Upvotes: 3

Views: 436

Answers (1)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52223

You are on the right track. All you need to do is to iterate through value as well:

{% for key, value in test.items %}
    {% for dct in value %}
        {% for k, sum in dct.items %}
            {{ key }}: {{ sum }} <br />
        {% endfor %}
    {% endfor %}
{% endfor %}

Upvotes: 3

Related Questions