通过Django模板中的json列表进行迭代

时间:2022-09-04 20:19:08

I am passing the following context in my Django template :

我在Django模板中传递以下上下文:

context = {'test': custom_json_list}

And the output of custom_json_list is this :

而且custom_json_list的输出是这样的:

{'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.

提前致谢。

1 个解决方案

#1


4  

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 %}

#1


4  

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 %}