2013-03-25 85 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