djangoformsredirect-after-post

django - passing information when redirecting after POST


I have a simple form, that when submitted redirects to a success page.

I want to be able to use the data that was submitted in the previous step, in my success page.

As far as I know, you can't pass POST data when redirecting, so how do you achieve this?

At the moment I'm having to just directly return the success page from the same URL, but this causes the dreaded resubmission of data when refreshed.

Is using request.session the only way to go?


Solution

  • You can:

    1. Pass the data (either full data or just id to object) in request.session
    2. Redirect with something like ?id=[id] in URL - where [id] points to your object.

    Update:

    Regarding pt. 1 above, I meant that you could do (in POST handler):

    my_object = MyModel.objects.create(...)
    request.session['my_object_id'] = my_object.id
    

    Or you could try passing the whole object (it should work but I'm not 100% certain):

    my_object = MyModel.objects.create(...)
    request.session['my_object'] = my_object