我想从URL获取ID,以便可以在diff中使用queryset。类的方法。但它显示:
schedule = get_object_or_404(Schedule, id=kwargs['pk'])
NameError: name 'kwargs' is not defined
这是我的代码:
class SubmitAttendanceView(View):
template_name = 'schedule/submit_attendance.html' # this html file will be included in 'schedule/scheduledetail.html'
form_class = AttendanceForm
schedule = get_object_or_404(Schedule, id=kwargs['pk'])
students = Student.objects.filter(course__id__in=schedule.course.all(), sem=schedule.sem, subject__id__contains=schedule.subject.id).order_by('roll_no')
def get(self, request, pk):
form = self.form_class()
return render(request, self.template_name, {'form': form, 'students': self.students})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
date = form.cleaned_data['lecture_date']
lecture = self.schedule.id
subject = self.schedule.subject.id
x = 1 # a counter to fetch each checkbox from template by their name
for student in self.students:
course = Course.objects.get(id=student.course.id)
mark = self.request.POST[f'mark{x}']
if not mark:
mark = 0
attendance = Attendance(lecture=lecture, subject=subject, course=course, student=student, lecture_date=date, mark=mark)
attendance.save()
x += 1
return redirect('schedule')
return render(request, self.template_name, {'form': form, 'students': students})
urls.py:
path('<int:pk>/submit/', SubmitAttendanceView.as_view(), name='submit-attendance')
在模板中:
<a class="btn btn-md btn-danger add-new my-2" type="button" href="{% url 'submit-attendance' schedule.pk %}">Submit Attendance</a>
还告诉我是否还有另一种方法可以将queryset传递给变量并在我的类方法中使用它
已解决:
class SubmitAttendanceView(View):
template_name = 'schedule/submit_attendance.html' # this html file will be included in 'schedule/scheduledetail.html'
form_class = AttendanceForm
def get_schedule(self, value):
return get_object_or_404(Schedule, id=value)
def get_students(self, value):
schedule = self.get_schedule(value)
# specify Students queryset
students_queryset = Student.objects.filter(course__id__in=schedule.course.all(), sem=schedule.sem, subject__id__contains=schedule.subject.id).order_by('roll_no')
return students_queryset
def get(self, request, **kwargs):
form = self.form_class()
students = self.get_students(kwargs['pk'])
return render(request, self.template_name, {'form': form, 'students': students})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
date = form.cleaned_data['lecture_date']
schedule = self.get_schedule(kwargs['pk'])
lecture = Schedule.objects.get(id=schedule.id)
subject = Subject.objects.get(id=schedule.subject.id)
x = 1 # a counter to fetch each checkbox from template by their name
students = self.get_students(kwargs['pk'])
for student in students:
course = Course.objects.get(id=student.course.id)
mark = self.request.POST.get(f'mark{x}')
if not mark:
mark = 0
attendance = Attendance(lecture=lecture, subject=subject, course=course, student=student, lecture_date=date, mark=mark)
attendance.save()
x += 1
return redirect('schedule')
return render(request, self.template_name, {'form': form, 'students': students})
进行上述代码更改后,它可以正常工作
答案 0 :(得分:1)
您不能在类属性中使用kwargs
,因为它是传递给您的视图的关键字参数。您只能在视图中使用它。
class SubmitAttendanceView(View):
template_name = 'schedule/submit_attendance.html' # this html file will be included in 'schedule/scheduledetail.html'
form_class = AttendanceForm
def get_schedule(self, **kwargs):
return get_object_or_404(Schedule, id=kwargs['pk'])
def get_students(self, **kwargs):
schedule = self.get_schedule(kwargs)
# specify Students queryset
students_queryset = Student.objects.filter(...)
return students_queryset
def get(self, request, **kwargs):
form = self.form_class()
students = self.get_students(kwargs)
return render(request, self.template_name, {'form': form, 'students': students})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
date = form.cleaned_data['lecture_date']
schedule = self.get_schedule(kwargs)
lecture = schedule.id
subject = schedule.subject.id
x = 1 # a counter to fetch each checkbox from template by their name
students = self.get_students(kwargs)
for student in self.students:
course = Course.objects.get(id=student.course.id)
mark = self.request.POST[f'mark{x}']
if not mark:
mark = 0
attendance = Attendance(lecture=lecture, subject=subject, course=course, student=student, lecture_date=date, mark=mark)
attendance.save()
x += 1
return redirect('schedule')
return render(request, self.template_name, {'form': form, 'students': students})