简体   繁体   English

Django如何检查元素是否在一周的某天,并且在一周的每一天仅显示一次按钮

[英]Django how to check if element is on a day of the week and display a button only once for each day of the week

The question has two parts to it. 这个问题分为两个部分。

I'm trying to display a button to bookmark certain days (ie Monday, Tuesday, Wednesday etc) if they exist in my context. 我试图显示一个按钮以将某些天(例如星期一,星期二,星期三等)添加为书签,如果它们存在于我的上下文中。

I'm not quite sure how to equate a datetime object to a day of the week, if I have something like: 我不太确定如何将日期时间对象等同于一周中的某天,如果我有类似以下内容的话:

events= {'event one': 7:45pm 27/11/2015
         'event two': 9:00pm 28/11/2015
         'event three': 10:00pm 28/11/2015
         'event four': 11:pm 30/11/2015 } 

I realise in a normal dictionary I could do events.contains(x) but because I'm trying to compare dates when I define x as 27/11/2015 it is difficult to compare as the time segment is defined as midnight. 我意识到在普通字典中我可以进行events.contains(x),但是由于我试图在将x定义为2015年11月11日时比较日期,因此很难比较,因为时间段被定义为午夜。

Secondly as I have two events on Saturday (28/11/2015) I only want to define the button once. 其次,由于我在周六(2015/11/28)有两个活动,所以我只想定义一次按钮。 I am unsure how to approach this at all. 我完全不确定该如何处理。

Perhaps I should be processing both of these questions in the view? 也许我认为应该同时处理这两个问题?

I would extract the days from the events and then use them for whatever processing. 我将从事件中提取日期,然后将其用于任何处理。 The snippet below will extract the day of the week from each event and add it to a set (so no repetitions). 下面的代码段将从每个事件中提取星期几并将其添加到集合中(因此不会重复)。 Each entry of the set will have its own button. 该集合的每个条目都有其自己的按钮。

days_set = set()
for event, time_of_event in events.items():
    days_set.add(time.strptime(time_of_event, "%I:%M%p %d/%m/%Y").tm_wday)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM