2017-03-07 59 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 %}