I have the following models:
class Event(Page)
# my fields
start_date = models.DateField()
end_date = models.DateField()
class EventsIndex(Page):
# my fields
def get_events(self):
events = Event.objects.all().sort_by("start_date")
return events
In my EventsIndex
template I have 4 tabs which show the current month, the next month, the month after next and then a final tab called "Later" which should display the individual Event
s grouped by month:
I'm trying to figure out the logic for which to get these events to display. The individual month tabs should only display the events, whereas the "Later" tab should display a heading of the month of the events and then the events themselves. Initially I thought about creating a function for each tab, so something like:
def current_month_events(self):
current_month, current_year = datetime.now().month, datetime.now().year
events = Event.objects.filter(start_date__year=current_year,
start_date__month=current_month).order_by("start_date")
return events
# Repeat for all required tabs
However this seems to go against the DRY principle. Is there a more pythonic way to handle this logic?