0

i am trying to make quiz app, i want make user to select subjects and topic and number of questions(genrate quiz). after input next view should be test/quiz .i am unable to pass the multiple parameter/values to django url. (e.g subject/topic/no_of_question)

def generate_quiz(request):
    if request.method == 'GET':
        subjects = Subject.objects.all()
        topics = Topic.objects.all()
        return render(request, 'mcq/quizetting.html', {'subjects':subjects,'topics':topics})

    if request.method == 'POST':
        subject_id = request.POST.get("subject", "")
        topic_id = request.POST.get("topic", "")
        mcq_no = request.POST.get("mcq_no", "")

        subjects = Subject.objects.all
        if subject_id != '':
            if topic_id != '':
                if mcq_no >= 10 and mcq_no <150:
                    return HttpResponseRedirect(reverse('test', kwargs=['subject':subject_id, ]))
                    
                else:
                    return render(request, 'mcq/quizetting.html', {'subjects':subjects,'topics':topics})
            else :
                topics = Subject.objects.get(id=subject_id).topic_set.all()
        else:
            topics = Topic.objects.all()
        subject_id = str(subject_id)
        return render(request, 'mcq/quizetting.html', {'subjects':subjects,'topics':topics, 'current_subject_id':subject_id})

def test(request, subject, topic, mcq_no):
    s = Subject.objects.get(id=subject)
    t = Topic.objects.get(id=topic)
    selected_questions = Question.objects.filter(subject=s, topic=t)[:mcq_no]
    

    return render(request, 'mcq/test.html', {'questions':selected_questions})

my view is working upto subject selection,topic filter and question filtration. when i submit form next url is not working. i have tired both {srt and int) variable to pass in url

path('generate_quiz',views.generate_quiz, name='generate_quiz'),
    path('test/<subjects_id>/<topics_id>/<int:mcq_no>',views.test, name='test'),
drnewbie
  • 11
  • 2
  • 1
    What would you imagine the resulting URL would look like? Could you do this with GET parameters? – Iain Shelvington Jan 05 '20 at 16:57
  • when i submit selection form (url /genrate_quiz) then next view should be test/ or paper (url e.g subject/topic/100 ) – drnewbie Jan 05 '20 at 17:26
  • Just use test/?subjects_id=your_subjects_id&topics_id=your_topics_id&mcq_no=your_mcq_no. To get query params do this request.query_params.get('subjects_id')so on...... – Gorkhali Khadka Jan 05 '20 at 17:38
  • Gorkhali Khadka dear i had issue with url tying to figuring that i tried to change view but failed – drnewbie Jan 05 '20 at 17:47

1 Answers1

0

after with the help of my friend i have solved problem. i was passing "mcq_no" as string after converting it into integer valye my problem has been solve. if int(mcq_no) >= 10 and int(mcq_no) <150:

my url was int:mcq_no

path('generate_quiz',views.generate_quiz, name='generate_quiz'),
path('test/<subject>/<topic>/<int:mcq_no>',views.test, name='test'),
drnewbie
  • 11
  • 2