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

When processing a POST request in the Django views.py file, I sometimes need to redirect it to another url. This url I'm redirecting to is handled by another function in the same Django views.py file. Is there a way of doing this and maintaining the original POST data?

UPDATE: More explanation of why I want to do this. I have two web apps (let's call them AppA and AppB) which accept data entered into a text field by the user. When the the user clicks submit, the data is processed and detailed results are displayed. AppA and AppB expect different types of data. Sometimes a user mistakenly posts AppB type data to AppA. When this happens I want to redirect them to AppB and show the AppB results or at least have it populated with the data they entered into AppA.

Also:

  • The client wants two separate apps rather than combining them into just one.

  • I can't show the code as it belongs to a client.

UPDATE 2: I've decided that KISS is the best principle here. I have combined the two apps into one which makes things simpler and more robust; I should be able to convince the client it's the best way to go too. Thanks for all the great feedback. If I was going to maintain two apps as described then I think sessions would be the way to do this - thanks to Matthew J Morrison for suggesting that. Thanks to Dzida as his comments got me thinking about the design and simplification.


Solution 1:

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).

Solution 2:

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.

Solution 3:

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

Solution 4:

use requests package.Its very easy to implement

pip install requests

then you can call any urls with any method and transfer data

in your views import requests

import requests

to post data, follow the format

r = requests.post('http://yourdomain/path/', data = {'key':'value'})

to get the absolute url in django view, use

request.build_absolute_uri(reverse('view_name'))

Thus the django view code looks like

r = requests.post(
            request.build_absolute_uri(reverse('view_name')), 
            data = {'key':'value'}
    )

where r is the response object with status_code and content attribute. r.status_code gives the status code(on success it will be 200) and r.content gives the body of response. There is a json method(r.json()) that will convert response to json format

requests

requests.post