How to redirect from one question to another having same forignkey (quiz name)

35 Views Asked by At

I m building quiz app I have 3 models ( Quiz, Question,Answers) Question foreign key with Quiz,and Answee foreign key with Questions. I m successful in showing each question on separate page according to their pk. But to redirect from one question to another until last question

1

There are 1 best solutions below

0
On

Most likely, you have to do something like this in your view:

def answer(request):
    # here should be code to handle answer

    question = Question.objects.get(pk=request.POST['id'])
    quiz = question.quiz
    later_question = Question.objects.filter(quiz_id=quiz.id, question_id__gt=question.id).first()

    if later_question:
        # redirect to later question
    else:
        # no later question, end the quiz