Django: How do I redirect a post and pass on the post data

If you faced such problem there's a slight chance that you might need to revise your designs.

This is a restriction of HTTP that POST data cannot go with redirects.

Can you describe what are you trying to accomplish and maybe then we can think about some neat solution.

If you do not want use sessions as Matthew suggested you can pass POST params in GET to the new page (consider some limitations such as security and max length of GET params in query string).

UPDATE to your update:) It sounds strange to me that you have 2 web apps and those apps use one views.py (am I right?). Anyway consider passing your data from POST in GET to the proper view (in case data is not sensitive of course).


I think how I would probably handle this situation would be to save the post data in session, then remove it when I no longer need it. That way I can access the original post data after a redirect even though that post is gone.

Will that work for what you're trying to do?

Here is a code sample of what I'm suggesting: (keep in mind this is untested code)

def some_view(request):
    #do some stuff
    request.session['_old_post'] = request.POST
    return HttpResponseRedirect('next_view')

def next_view(request):
    old_post = request.session.get('_old_post')
    #do some stuff using old_post

One other thing to keep in mind... if you're doing this and also uploading files, i would not do it this way.


You need to use a HTTP 1.1 Temporary Redirect (307).

Unfortunately, Django redirect() and HTTPResponseRedirect (permanent) return only a 301 or 302. You have to implement it yourself:

from django.http import HttpResponse, iri_to_uri
class HttpResponseTemporaryRedirect(HttpResponse):
    status_code = 307

    def __init__(self, redirect_to):
        HttpResponse.__init__(self)
        self['Location'] = iri_to_uri(redirect_to)

See also the django.http module.

Edit:

on recent Django versions, change iri_to_uri import to:

from django.utils.encoding import iri_to_uri

Tags:

Django