2013-03-25 84 views
1

我想要参加测验,如果您回答错误的问题,您会看到该问题会被添加到错误问题列表中。动态视图不显示

问题,可以在多个测验中使用,所以我不能硬编码问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}) 
+0

能否请您分享right.html内容的名字吗? – 2013-03-25 16:26:23

+0

它包含“正确”,所以我知道我做了测验 – Siecje 2013-03-25 16:52:47

回答

0

如果您计划托管这并在网上提供,你应该重新开始。您不应该使用这样的列表来存储内容,您应该使用sessionscache。此外,您最好使用FormWizard进行测验,因为它旨在完成您想要做的事情:处理多个表单并处理它们。

+0

我正在看FormWizard,我想知道。你如何设置表单域的选择?你如何使用相同的表单多个动态时间。显然,你不能用FormWizard使用ajax,这仍然是这样吗? – Siecje 2013-03-26 17:50:11

+0

@Siecje所以FormWizard有一些方法,你可以重写来设置这样的东西。您可能需要['get_form'](https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/#django.contrib.formtools.wizard.views.WizardView.get_form)方法。至于多次使用相同的表单,您可能只需动态创建一个包含要传递给视图的表单副本的列表。我不确定ajax,你可以制作一个使用ajax的模板,我想不出任何会阻止你此刻做的事情。 – Ngenator 2013-03-26 18:07:41

0

也许你应该尝试

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

到视图重定向到另一个视图。

P.S .:反转应该包含你的“answer_question”鉴于

+0

我同意Ngenator在会话或缓存中的使用。 – Erika 2013-03-25 17:20:07

+0

将'answer_question'与参数'(u'1',1,['test'])'和关键字参数'{}'找不到“相反。 – Siecje 2013-03-25 17:37:58

+0

就像我说过的,'answer_question'应该替换为你在urls.py中的名称,这会导致这种观点。 – Erika 2013-03-25 17:49:18