0

i have written alot of class based views, and also configured it's urls but this particluar view is showing this error AttributeError: 'function' object has no attribute 'as_view' i cannot tell what is going on with the view

urls.py

path('<slug:course_slug>/<slug:quiz_slug>/results/', views.QuizResultsView.as_view(), name="quiz_results"),

views.py

@method_decorator([login_required, teacher_required], name='dispatch')
class QuizResultsView(DetailView):
    model = Quiz
    context_object_name = 'quiz'
    template_name = 'classroom/teachers/quiz_results.html'

    def get_context_data(self, **kwargs):
        quiz = self.get_object()
        course = Course.objects.get(slug=course_slug)
        quiz = Quiz.objects.get(slug=quiz_slug, course=course)
        taken_quizzes = quiz.taken_quizzes.select_related('student__user').order_by('-date')
        total_taken_quizzes = taken_quizzes.count()
        quiz_score = quiz.taken_quizzes.aggregate(average_score=Avg('score'))
        extra_context = {
            'taken_quizzes': taken_quizzes,
            'total_taken_quizzes': total_taken_quizzes,
            'quiz_score': quiz_score,
            'total_questions':quiz.questions.count()
        }
        kwargs.update(extra_context)
        return super().get_context_data(**kwargs)

    def get_queryset(self):
        self.kwargs['course_slug']
        self.kwargs['quiz_slug']
        return self.request.user.quizzes.all()
3
  • 1
    Does this answer your question? stackoverflow.com/questions/70540773/… Commented Jun 13, 2022 at 12:27
  • try to remove method_decorator top of it Commented Jun 13, 2022 at 12:28
  • because method_decorator is a function and you inherit your class to a function this is why you get a "function" called error Commented Jun 13, 2022 at 12:29

1 Answer 1

2

use LoginRequiredMixin instead of login_required decorator. login_required returns a function and as_view specified only for classes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.