I'm tyring to use this snippet as my event calendar. Unfortunately it constantly raises new errors. Currently I get:
TemplateSyntaxError at /event/while-rendering-nothing-repeat/
Caught an exception while rendering: 'NoneType' object has no attribute 'date'
I'm pretty sure it comes from the line :
if day >= event.date.date() and day <= event.end.date():
Since when I change date()
to for example end__day
it raises "Event object has no...blah blah"
.
Here's my modified templatetag :
from datetime import date, timedelta
import calendar
from django import template
from fandrive.event.models import Event
register = template.Library()
def get_last_day_of_month(year, month):
if (month == 12):
year += 1
month = 1
else:
month += 1
return date(year, month, 1) - timedelta(1)
def month_cal(year=date.today().year, month=date.today().month):
event_list = Event.objects.filter(date__year=year, date__month=month)
first_day_of_month = date(year, month, 1)
last_day_of_month = get_last_day_of_month(year, month)
first_day_of_calendar = first_day_of_month - timedelta(first_day_of_month.weekday())
last_day_of_calendar = last_day_of_month + timedelta(7 - last_day_of_month.weekday())
month_cal = []
week = []
week_headers = []
i = 0
day = first_day_of_calendar
while day <= last_day_of_calendar:
if i < 7:
week_headers.append(day)
cal_day = {}
cal_day['day'] = day
cal_day['event'] = False
for event in event_list:
if day >= event.date.date() and day <= event.end.date():
cal_day['event'] = True
if day.month == month:
cal_day['in_month'] = True
else:
cal_day['in_month'] = False
week.append(cal_day)
if day.weekday() == 6:
month_cal.append(week)
week = []
i += 1
day += timedelta(1)
return {'calendar': month_cal, 'headers': week_headers}
register.inclusion_tag('static/calendarBox.html')(month_cal)
Event model :
class Event(models.Model):
slug = models.SlugField(max_length=255, unique=True, verbose_name='Slug')
date = models.DateTimeField()
end = models.DateTimeField()
And the template is the same as within the snippet code. Why on earth my Event.date has no date() function? Btw anyone knows why when according to comments for the snippet I was trying to use monthrange it threw 'module has no monthrange function' ?