i am working with a django quiz application where one question per page. when i will answer one question selecting a radio button and click submit button how can i get the next question in the next page as well as the answer will submitted to database. If anyone help me it would be an outstanding solution for me. please I wrote a view to get the question and render the answer in the radio link . but when i submit the answer how can i map the url to get dynamically new question each. thank you so much. I need a simplest solution please ..
A:
You can use HttpResponseRedirect to point from the currently submitted page to the next. For example (for simplicity, I assume you have a question model that points to the next question for each question):
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})
OmerGertel
2010-10-10 18:11:30
Thank you so much for giving this valuable answer.
paul
2010-10-11 04:31:00
A:
Did you considered displaying the question into a QuestionForm and then having multiple forms into a FormWizard? It will look similar to steps needed to install a program. You have the next steps until the finish step.
Seitaridis
2010-10-11 12:49:24