【问题标题】:Comparing time between 2 lists in Django Template比较Django模板中2个列表之间的时间
【发布时间】:2017-07-27 01:30:54
【问题描述】:

我知道逻辑可能有问题,我已经研究了一段时间,现在无法思考,真的需要一些帮助。

我基本上是想在我的 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

【问题讨论】:

  • 您应该将逻辑移动到视图中,而不是模板中。在 Python 中完成所有这些,然后渲染。你需要如何展示它?每个活动的时间?
  • 查看更多数据会有所帮助。 my_workouts 包含什么内容?
  • @AndreyShipilov 我的帖子中有一个示例,说明我希望如何显示它。基本上显示一个小时的时间段是否已满,如果不是……我想显示一个书本按钮
  • @Carl my_workouts 包含当天已经预订的活动的对象列表,而 time_slots 包含从 09:00 到 17:00 的时间列表 .... 我想基本上匹配 2 之间的时间并显示 time_slot 是满还是空
  • 所以你想显示EventExists,即使可能同时有[Book]?对吗?

标签: python django for-loop django-templates django-views


【解决方案1】:

在你的 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 %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 2021-12-31
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 2018-12-28
    相关资源
    最近更新 更多