arrays - 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 %}
如果数据匹配,我需要这样的结果,我有活动课程
解决方案
与其遍历时间列表,不如在数据库查询中使用该列表。
如果您有一个列表,您可以__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)
推荐阅读
- sql-server - SQL Server:从调用其他存储过程的存储过程中捕获临时表
- python - 在 DjangoModelFactory 中使用 Django faker 创建的克隆模型字段
- javascript - TypeScript 针对错误的 ECMAScript 版本
- firebase-realtime-database - firebase 功能 INVALID_ARGUMENT HTTP 错误 400 在部署
- javascript - 在输入 4 位密码时删除最后一个字母的字母间距?
- r - 如何根据第一个数据帧中的日期顺序合并数据帧,从第二个数据帧中选择一个字段
- javascript - Serial API 中的 navigator.serial 和 SerialPort 上的函数是否未实现?
- javascript - javascript 平台(或 REST API)的 HERE 地图中是否有与重新路由相关的可用功能?
- rxjs - 将 .groupBy 与 Firestore 收集数据一起使用
- javascript - 如何使用 JavaScript 在另一个表中创建一个表?