Django应用中的HTMLCalendar:缺少参数

时间:2019-07-01 13:15:20

标签: python django

我正在尝试在我的Django应用中复制此Link

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', '&nbsp;')

    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>

运行此命令会导致

  

calendar()缺少2个必需的位置参数:“年”和“月”

与链接中提供的代码不同,我在做什么?

跟踪

跟踪:

  

文件   “ C:\ Users \ Sid \ Anaconda3 \ lib \ site-packages \ django \ core \ handlers \ exception.py”   在内部     35. response = get_response(request)

     

文件   “ C:\ Users \ Sid \ Anaconda3 \ lib \ site-packages \ django \ core \ handlers \ base.py”   在_get_response中     128. response = self.process_exception_by_middleware(e,request)

     

文件   “ C:\ Users \ Sid \ Anaconda3 \ lib \ site-packages \ django \ core \ handlers \ base.py”   在_get_response中     126. response = wrapd_callback(request,* callback_args,** callback_kwargs)

     

异常类型:/ shipper / abc /处的TypeError异常值:calendar()   缺少2个必需的位置参数:“年”和“月”

1 个答案:

答案 0 :(得分:1)

您必须在year中添加monthsurls.py作为参数

path('abc/<int:year>/<int:month>/', name='cal'),

如果您想为每年和每个月创建一个日历,那么您将不得不遍历每年和每个月

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)