Search code examples
pythondjangosessionviewrequest

request.session is not transferring full querydict to another view


I am submitting a form using 'post' and transferring its data to another view using request.POST but my querydict is incomplete when it arrives in the second view.

view1

def question_filter(request):
    if request.method == 'POST':
        print('before validation', request.POST)
        request.session['question_data'] = request.POST
        return HttpResponseRedirect(reverse('qapp:question_preview'))

view2

def question_preview(request):
    all_questions = Questions.objects.all()
    question_data = request.session.get('question_data')
    print(question_data)
    question_pk_list = question_data['question_pk']
    preview_questions = all_questions.filter(id__in=question_pk_list)
    ...
    return render(request,'apps/qapp/question_preview.html', {somecontext})

Am I doing something wrong here ?

Update:

before validation <QueryDict: {'topics_all': ['1', '2'], 'csrfmiddlewaretoken': ['...'], 'subtopics_all': ['4', '2'], 'classroom': ['3'], 'difficulty': ['l', 'm']}>
[28/Feb/2018 17:17:39] "POST /question/filter/ HTTP/1.1" 302 0

(in the second view)question data {'topics_all': '2', 'csrfmiddlewaretoken': '...', 'difficulty': 'm', 'subtopics_all': '2', 'classroom': '3'}

Solution

  • you cannot send all the post data like that, as you have list inside your input names, you have to access each names differently and set them in the session.

    request.session['question_data'] = request.POST.getlist('topics_all')
    

    same for other input names, then access with key in the second view