首页 > 解决方案 > Django检查数组内的数组并更改模板

问题描述

我有这个数组线

setime = ["00:00", "00:30", "01:00", "01:30", "02:00", "02:30", "03:00", "03:30", "04:00", "04:30", "05:00", "05:30", "06:00", "06:30", "07:00", "07:30", "08:00", "08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30"] 

我要检查

在视野中

 myListTime= checkDateTime.objects.all().filter(Teacher_id = 1)

我在模板中试过

{% for i in setime %}
                <ul>
                    {% for j in myListTime %}
                        {% if j.cuTime in i %}
                            <li class="active">{{ i }}</li>
                        {% else %}
                            <li>{{ i }}</li>
                        {% endif %}
                    {% endfor %}
                </ul>
                {% endfor %}

如果数据匹配,我需要这样的结果,我有活动课程

在此处输入图像描述

在数据库中

标签: arraysdjangodjango-templatesdjango-template-filters

解决方案


与其遍历时间列表,不如在数据库查询中使用该列表。

如果您有一个列表,您可以__in检查字段值中是否存在值列表​​。

setime = ["00:00", "00:30", ..., "22:30", "23:00", "23:30"] 

myListTime_with_time = checkDateTime.objects.all().filter(Teacher_id=1, cuTime__in=setime)

myListTime_without_time = checkDateTime.objects.all().filter(Teacher_id=1).exclude(cuTime__in=setime)

推荐阅读