我正在尝试进行测验,如果您回答错误的问题,您会看到该问题被添加到错误问题列表中。
问题可以在多个测验中使用,所以我不能硬编码询问 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})