[Django]-Django – How to pass a form instance from one view to another

4👍

Well, you can store form’s cleaned_data in django session and pass it to another form as initial. For example:

def first_view(request):
   form = FirstForm(request.POST)
   if form.is_valid():
       request.session['first_form'] = form.cleaned_data
       return redirect('to_second_view')

def second_view(request):
     form = SecondForm(request.POST)
     if form.is_valid():
        first_form_data = request.session.pop('first_form',{})
        first_form_instance = FirstFormModel.objects.create(**first_form_data)
        second_form_instance = form.save()
     # rest of the code...
👤ruddra

Leave a comment