2010-10-10 53 views
0

我正在與一個Django測驗應用程序每頁一個問題。當我回答一個問題時,選擇一個單選按鈕並單擊提交按鈕,我如何在下一頁中獲得下一個問題以及答案將提交給數據庫。如果有人幫助我,這將是一個傑出的解決方案。請 我寫了一個視圖來獲取問題並在無線電鏈接中呈現答案。但是當我提交答案時,我如何映射網址以獲得動態的新問題。非常感謝。 我需要一個最簡單的解決方案..如何在每次提交表單時獲得下一個表單/頁面?

回答

0

您可以使用HttpResponseRedirect指向從當前提交的頁面到下一個。 例如(爲簡單起見,我假設你有一個問題模型指向下一個問題,每題):

def view_question(request,id): 
    q = Question.objects.get(id) 
    if request.method == 'POST': 
     form = QuestionForm(request.POST) 
     if form.is_valid(): 
      # this takes care of saving the answer 
      a = Answer.objects.create(user=request.user, question=q, a = form.cleaned_data['answer']) 
      # this takes care of moving on to the next question 
      if q.next_question != None: 
       return HttpResponseRedirect(q.next_question.get_absolute_url()) 
      # no next question, finished quiz 
      return HttpResponseRedirect('/done-quiz/') 
    else: 
     form = QuestoinForm() 

    return render_to_response('quiz.html', {'form': form, 'question':q}) 
+0

非常感謝你給了這個寶貴的答案。 – paul 2010-10-11 04:31:00

相關問題