尝试在django-quiz应用程序中进行测验时出错

时间:2016-04-03 17:02:54

标签: django python-3.x

当我尝试进行测验时,它会给我一个错误:

    Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/quiz/testing-your-quiz/take/

Django Version: 1.9.4
Python Version: 3.5.1
Installed Applications:
['student',
 'module_content',
 'announcement',
 'module_progress',
 'coverage',
 'quiz',
 'multichoice',
 'true_false',
 'essay',
 'todo',
 'bootstrapform',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.facebook',
 'allauth.socialaccount.providers.google',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Users\Randy\Envs\website\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "C:\Users\Randy\Envs\website\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\Randy\Envs\website\lib\site-packages\django\views\generic\base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "C:\Users\Randy\Envs\website\tsl\quiz\views.py" in dispatch
  155.         return super(QuizTake, self).dispatch(request, *args, **kwargs)

File "C:\Users\Randy\Envs\website\lib\site-packages\django\views\generic\base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "C:\Users\Randy\Envs\website\lib\site-packages\django\views\generic\edit.py" in get
  213.         return self.render_to_response(self.get_context_data())

File "C:\Users\Randy\Envs\website\tsl\quiz\views.py" in get_context_data
  190.         context = super(QuizTake, self).get_context_data(**kwargs)

File "C:\Users\Randy\Envs\website\lib\site-packages\django\views\generic\edit.py" in get_context_data
  122.             kwargs['form'] = self.get_form()

File "C:\Users\Randy\Envs\website\lib\site-packages\django\views\generic\edit.py" in get_form_with_form_class
  35.                     return get_form(self, form_class=form_class)

File "C:\Users\Randy\Envs\website\tsl\quiz\views.py" in get_form
  168.         return form_class(**self.get_form_kwargs())

File "C:\Users\Randy\Envs\website\tsl\quiz\forms.py" in __init__
  8.         choice_list = [x for x in question.get_answers_list()]

Exception Type: AttributeError at /quiz/testing-your-quiz/take/
Exception Value: 'bool' object has no attribute 'get_answers_list'

问题是我尝试撤消对编码所做的所有更改,但它仍然给我错误,我不知道原因。

以下是此错误所涉及的相关文件:

class QuizTake(FormView):
    form_class = QuestionForm
    template_name = 'question.html'

    def dispatch(self, request, *args, **kwargs):
        self.quiz = get_object_or_404(Quiz, url=self.kwargs['quiz_name'])
        if self.quiz.draft and not request.user.has_perm('quiz.change_quiz'):
            raise PermissionDenied

        self.logged_in_user = self.request.user.is_authenticated()

        if self.logged_in_user:
            self.sitting = Sitting.objects.user_sitting(request.user,
                                                        self.quiz)
        else:
            self.sitting = self.anon_load_sitting()

        if self.sitting is False:
            return render(request, 'single_complete.html')

        return super(QuizTake, self).dispatch(request, *args, **kwargs)

另一个片段:

def get_context_data(self, **kwargs):
        context = super(QuizTake, self).get_context_data(**kwargs)
        context['question'] = self.question
        context['quiz'] = self.quiz
        if hasattr(self, 'previous'):
            context['previous'] = self.previous
        if hasattr(self, 'progress'):
            context['progress'] = self.progress
        return context

还有forms.py:

from django import forms
from django.forms.widgets import RadioSelect, Textarea


class QuestionForm(forms.Form):
    def __init__(self, question, *args, **kwargs):
        super(QuestionForm, self).__init__(*args, **kwargs)
        choice_list = [x for x in question.get_answers_list()]
        self.fields["answers"] = forms.ChoiceField(choices=choice_list,
                                                   widget=RadioSelect)


class EssayForm(forms.Form):
    def __init__(self, question, *args, **kwargs):
        super(EssayForm, self).__init__(*args, **kwargs)
        self.fields["answers"] = forms.CharField(
            widget=Textarea(attrs={'style': 'width:100%'}))
是的,有人能帮帮我吗?我真的不知道如何解决它。

1 个答案:

答案 0 :(得分:0)

实例化时,您需要将question传递给QuestionForm。这可能应该在您的视图的get_form_kwargs方法中完成。