我知道这个逻辑可能存在一些问题,而且我已经有一段时间没有做过这个问题了,现在不能直接思考,真的可以使用一些帮助。
我基本上试图在我的Django模板中打印出任何给定日期的事件列表。我遇到的问题是我在任何一天都有多个事件。具有事件(来自time_slots
)的my_workouts
打印出book button
以及第二次或第三次通过循环。
{% for slot in time_slots %}
{{ slot.time }}
{% for event in my_workouts %}
{% ifchanged %}
{% if event.start_time <= slot.time and event.end_time > slot.time %}
EventExists
{% else %}
<button type="button" class="btn">Book</button>
{% endif %}
{% endifchanged %}
{% endfor %}
{% endfor %}
目前打印为......
09:00 EventExists [Book]
10:00 [Book]
11:00 [Book]
12:00 [Book] EventExists
我希望将其打印为......
09:00 EventExists
10:00 [Book]
11:00 [Book]
12:00 EventExists
答案 0 :(得分:0)
:
for ev in events:
ev.IsEvetExits = False
if ((event.start_time <= slot.time) and (event.end_time > slot.time)):
ev.IsEventExits = True
然后在你的模板中写:
{% for slot in time_slots %}
{{ slot.time }}
{% for event in my_workouts %}
{% ifchanged %}
{% if Event.IsEventExits %}
EventExists
{% else %}
<button type="button" class="btn">Book</button>
{% endif %}
{% endifchanged %}
{% endfor %}
{% endfor %}