1

我正在尝试进行测验,如果您回答错误的问题,您会看到该问题被添加到错误问题列表中。

问题可以在多个测验中使用,所以我不能硬编码询问 question_1 然后 question_2 等。

当我加载页面时,我看到了第一个问题,但是如果我提交或刷新,我会看到 right.html 模板。它不会问所有的问题。

它应该在呈现 right.html 页面之前询问所有问题

for question in quiz.questions.all():
   if question not in asked_questions:
        asked_questions.append(question)
        return answer_question(request, quiz_id, question.id, module_list)

模型.py

class Choice(models.Model):
    choice = models.CharField(max_length=64)
    def __unicode__(self):
        return self.choice

#create a multiple choice quiz to start
class Question(models.Model):
    question = models.CharField(max_length=64)
    answer = models.CharField(max_length=64)
    choices = models.ManyToManyField(Choice)
    module = models.CharField(max_length=64)

    def __unicode__(self):
        return self.question

class Quiz(models.Model):
    name = models.CharField(max_length=64)
    questions = models.ManyToManyField(Question)

    def __unicode__(self):
        return self.name

视图.py

asked_questions = []
module_list = []
module_list.append('test')

def take_quiz(request, quiz_id):
    quiz = Quiz.objects.get(id=str(quiz_id))

    for question in quiz.questions.all():
        if question not in asked_questions:
            asked_questions.append(question)
            return answer_question(request, quiz_id, question.id, module_list)
    #quiz is over
    return render (request, 'right.html')

def answer_question(request, quiz_id, question_id, module_list):
    question = Question.objects.get(id=question_id)
    #module_list = []
    if request.method == 'POST':
        form = QuestionForm(request.POST)
        choices = [(i, i) for i in question.choices.all()]
        form.fields['selection'].choices = choices

        if form.is_valid():
            if form.cleaned_data['selection'] != str(question.answer):
                module_list.append(question.module)
            return take_quiz(request, quiz_id)
    else:
        form = QuestionForm()
        choices = [(i, i) for i in question.choices.all()]
        form.fields['selection'].choices = choices

    return render(request, 'answer_question.html', {'question':question, 'form':form, 'module_list':module_list})
4

2 回答 2

0

也许你应该试试

return HttpResponseRedirect(reverse('answer_question', args=[quiz_id, question_id, module_list]))

将您的视图重定向到另一个视图。

ps:reverse 应该包含你的“answer_question”视图的名称

于 2013-03-25T17:18:06.787 回答
0

如果您打算托管它并使其在网络上可用,那么您真的应该重新开始。您不应该使用这样的列表来存储东西,而应该使用会话缓存。此外,最好使用FormWizard进行测验,因为它旨在完成您正在尝试做的事情:处理多个表单并处理它们。

于 2013-03-25T17:11:30.353 回答