开发者

how can i get a next form/page while each submitting form?

i am working with a django quiz application where one questio开发者_运维知识库n 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 ..


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})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜