我想要參加測驗,如果您回答錯誤的問題,您會看到該問題會被添加到錯誤問題列表中。動態視圖不顯示
問題,可以在多個測驗中使用,所以我不能硬編碼問question_1然後question_2等
當我打開我看到的第一個問題,但這時如果我提交或刷新我看到右邊的頁面。 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)
models.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
views.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})
能否請您分享right.html內容的名字嗎? – 2013-03-25 16:26:23
它包含「正確」,所以我知道我做了測驗 – Siecje 2013-03-25 16:52:47