I am trying to access an attribute from a model object that I passed to my class. So I have my view doing something like:
my_events = Event.objects.order_by('start').filter(start__year=year, start__month=month)
and then I pass that to my class like so:
calendar = EventCalendar(my_events).formatmonth(year, month)
and then here is my class
# -*- coding: UTF-8 -*-
from calendar import HTMLCalendar
from datetime import date
from itertools import groupby
from django.utils.html import conditional_escape as esc
class EventCalendar(HTMLCalendar):
def __init__(self, event):
super(EventCalendar, self).__init__()
self.event = self.group_by_day(event)
def formatday(self, day, weekday):
if day != 0:
cssclass = self.cssclasses[weekday]
if date.today() == date(self.year, self.month, day):
cssclass += ' today'
if day in self.event:
cssclass += ' filled'
extra_div = ['<div class="event_container"><ul>']
for event in self.event[day]:
extra_div.append('<li>')
extra_div.append('<a href="%s">' % event.get_absolute_url())
extra_div.append(esc(event.title))
extra_div.append('</a></li>')
extra_div.append('</ul></div>') #END event_container
#Custom div wrapper
extra_div.append('<div class="extra_container">')
#Custom div add_event
extra_div.append('<div class="add_event">')
extra_div.append('<a href="#">add event</a>')
extra_div.append('</div>')
#Custom div show_day
extra_div.append('<div class="show_day">')
extra_div.append('<a href="#">show day</a>')
extra_div.append('</div>')
extra_div.append('</div>') #END extra_container
return self.day_cell(cssclass, '%d %s' % (day, ''.join(extra_div)))
#Custom div wrapper
extra_div = ['<div class="extra_container">']
#Custom div add_event
extra_div.append('<div class="add_event">')
extra_div.append('<a href="#">add event</a>')
extra_div.append('</div>')
#Custom div show_day
extra_div.append('<div class="show_day">')
extra_div.append('<a href="#">show day</a>')
extra_div.append('</div>')
extra_div.append('</div>')
return self.day_cell(cssclass, '%d %s' % (day, ''.join(extra_div)))
return self.day_cell('noday', ' ')
def formatmonth(self, year, month):
self.year, self.month = year, month
return super(EventCalendar, self).formatmonth(year, month)
def group_by_day(self, event):
event_dict = {}
start = event.start.day
end = event.end.day
return event_dict
def day_cell(self, cssclass, body):
return '<td class="%s">%s</td>' % (cssclass, body)
The issue is with the group_by_day(self, event)
section in the code. I'm trying to retrieve the start and end day of my event. with:
start = event.start.day
end = event.end.day
But when I do that I get and error
'QuerySet' object has no attribute 'start'
Does this mean that when I retrieved the object I only got a reference to it, something like lazy instantiation ? Or am I just trying to access it wrong?
sorry if question is trivial, really new to python/django. Thanks
Returns a list of matching
Event
objects. You then pass this QuerySet list to:Which from your parameter names suggests you think you are passing a single event. So it doesn't work.