2017-03-07 70 views
0

我知道逻辑中可能存在某些问题,我一直在研究这个问题一段时间,现在无法直接思考,可以真正使用一些帮助。在Django中比较2个列表之间的时间模板

我基本上试图打印出我的Django模板中任何给定日期的事件列表。我遇到的问题是在任何一天有多个事件。有一个事件(来自my_workouts)的time_slots打印出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 
+2

您应该将逻辑移入视图中,而不是模板中。在Python中完成所有这些,然后渲染。你需要如何显示它?每个事件的时间? –

+0

这将有助于看到更多的数据。 my_workouts包含什么? – Carl

+0

@AndreyShipilov我在我的帖子中有一个例子,我希望如何显示它。基本上显示小时时间段是否已满,如果没有...我想显示书籍按钮 – capeman

回答

0
在view.py

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 %}