首页 > 解决方案 > Django - 在多个值的两个列表中进行迭代,仅返回一组迭代

问题描述

我有一个与 zip() 和迭代有关的问题。

nutritional_info_quantity_per_user = []

if related_profiles:
            for r in related_profiles:
                r_perc = round((r.kcal/self.request.user.profile.kcaltotal)*100)
                for intake in intakes:
                    intake_quantity = intake.amount
                    intake_custom_quantity_per_user = myround_two((intake_quantity/menu_kcal) * (kcal)*(r_perc/100))
                    nutritional_info_quantity_per_user.append(intake_custom_quantity_per_user)


(...)        
nutritional_infos_per_user = zip(intakes, nutritional_info_quantity_per_user)
(...)

查询很好,因为它们返回完整的值列表。

但是当我在模板中循环遍历“nutritional_infos_per_user”时,如下所示:

{% for t, i in nutritional_infos_per_user %}
{{t.type.name}}
{{i}}
{% endfor%}

我只得到一次迭代(但是我有两个或更多“related_profiles”)。显然我做错了什么,如果有人能提供帮助,我将不胜感激!

非常感谢。

标签: pythondjangodjango-viewsdjango-templates

解决方案


推荐阅读