I am trying to replicate this Link in my Django app
Views.py
from calendar import HTMLCalendar
from datetime import date
from itertools import groupby
from django.utils.html import conditional_escape as esc
from django.shortcuts import render_to_response
from django.utils.safestring import mark_safe
class WorkoutCalendar(HTMLCalendar):
def __init__(self, workouts):
super(WorkoutCalendar, self).__init__()
self.workouts = self.group_by_day(workouts)
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.workouts:
cssclass += ' filled'
body = ['<ul>']
for workout in self.workouts[day]:
body.append('<li>')
body.append('<a href="%s">' % workout.get_absolute_url())
body.append(esc(workout.title))
body.append('</a></li>')
body.append('</ul>')
return self.day_cell(cssclass, '%d %s' % (day, ''.join(body)))
return self.day_cell(cssclass, day)
return self.day_cell('noday', ' ')
def formatmonth(self, year, month):
self.year, self.month = year, month
return super(WorkoutCalendar, self).formatmonth(year, month)
def group_by_day(self, workouts):
field = lambda workout: workout.performed_at.day
return dict(
[(day, list(items)) for day, items in groupby(workouts, field)]
)
def day_cell(self, cssclass, body):
return '<td class="%s">%s</td>' % (cssclass, body)
def calendar(request, year, month):
my_workouts = Quiz.objects.orderby('scheduled_date').filter(owner_id=request.user.pk, scheduled_date__year=year, scheduled_date__month=month).annotate(c=Sum('weight')).values('c')
print("my_workouts", my_workouts)
cal = WorkoutCalendar(my_workouts).formatmonth(year, month)
return render_to_response('classroom/teachers/graph_tot_trucks.html', {'calendar': mark_safe(cal),})
Urls.py
path('abc/', teachers.calendar, name='cal'),
foo.html
<head>
{{calendar}}
</head>
When I run this it results in
calendar() missing 2 required positional arguments: 'year' and 'month'
What is it that I am doing differently than the code provided in the link?
Traceback
Traceback:
File "C:\Users\Sid\Anaconda3\lib\site-packages\django\core\handlers\exception.py" in inner 35. response = get_response(request)
File "C:\Users\Sid\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response 128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Sid\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response 126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /shipper/abc/ Exception Value: calendar() missing 2 required positional arguments: 'year' and 'month'
You have to add year
and months
as parameters in urls.py
path('abc/<int:year>/<int:month>/', name='cal'),
If you want to create a calendar for each year and month, then you will have to loop through each year and each month
years = set([quiz.scheduled_date.year for quiz in Quiz.objects.all()])
months = set([quiz.scheduled_date.month for quiz in Quiz.objects.all()])
quizzes = []
for year in years:
for month in months:
quiz = Quiz.objects.orderby('scheduled_date').filter(owner_id=request.user.pk, scheduled_date__year=year, scheduled_date__month=month).annotate(c=Sum('weight')).values('c')
quizzes.append(quiz)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With