Django 2.2//蟒蛇 3.6//我有一个使用“列表”和“字典”组合创建的上下文。数据结构如下所示。{ "Mike":[ { "month":"2020-02", "consult_counts_total":2, "consult_counts_total_uid":2 }, { "month":"2020-01", "consult_counts_total":4, "consult_counts_total_uid":7 }, { "month":"2019-12", "consult_counts_total":6, "consult_counts_total_uid":1 } ], "Jaden":[ { "month":"2020-02", "consult_counts_total":8, "consult_counts_total_uid":12 }, { "month":"2020-01", "consult_counts_total":23, "consult_counts_total_uid":11 }, { "month":"2019-12", "consult_counts_total":2, "consult_counts_total_uid":19 } ], "Sarah":[ { "month":"2020-02", "consult_counts_total":2, "consult_counts_total_uid":2 }, { "month":"2020-01", "consult_counts_total":4, "consult_counts_total_uid":7 }, { "month":"2019-12", "consult_counts_total":6, "consult_counts_total_uid":1 } ], "John":[ { "month":"2020-02", "consult_counts_total":1, "consult_counts_total_uid":0 }, { "month":"2020-01", "consult_counts_total":2, "consult_counts_total_uid":7 }, { "month":"2019-12", "consult_counts_total":5, "consult_counts_total_uid":1 } ]}我正在尝试通过模板中的循环显示此数据。首先我试过了。它显示了不错的结果。{% for foo in context_data %} <p>{{ foo }}</p>{% endfor %}# resultMikeJadenSarahJohn但是我无法获得更深入的数据。例如,我想得到迈克的所有月份(2020-02,2020-01,2019-12)
1 回答
江户川乱折腾
TA贡献1851条经验 获得超5个赞
您只迭代键,应使用项到字典中迭代值。
{% for key, values in context_data.items %}
<p class="big">This is {{ key }}'s months.</p>
{% for foo2 in values %}
<p class="small">{{ foo2.month }}</p>
{% endfor %}
<br>
{% endfor %}
添加回答
举报
0/150
提交
取消