简体   繁体   中英

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.

Secondly as I have two events on Saturday (28/11/2015) I only want to define the button once. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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