tags:

views:

337

answers:

5

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.

+2  A: 

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?

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.

Matthew J Morrison
I've never used sessions, but I'll have a look at that thanks.
swisstony
+1  A: 

If you faced such problem there's slight chance that you had over-complicated your design.

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

Lukasz Dziedzia
I can see what what he is trying to do could be valid if he is trying to handle an expired login that is going to force a user to login after submitting a form... in that case, he is going to want to retain the data that was submitted and not force the user to re-enter everything after they complete the login screen.
Matthew J Morrison
Not sure if I got the point but in this case login operation can be performed by first view with little code modification and without making unnecessary redirects. It would be great to read exisitng code to make more accurate advices.
Lukasz Dziedzia
I'm saying if you submit a form, and you're not logged in, you're going to get redirected to a login form... in that scenario, you'll lose whatever it was that you submitted. I agree about being able to see some existing code.
Matthew J Morrison
A: 

There's an included application in contrib called Redirects: http://docs.djangoproject.com/en/1.2/ref/contrib/redirects/

Andrew Sledge
-1, sorry. The django redirect app handles redirecting for 404s, this doesn't apply to what @swisstony is trying to accomplish.
Matthew J Morrison
A: 

If you are using a redirect after processing the POST to AppB, you can actually get away with calling the AppB method from the AppA method.

An Example:

def is_appa_request(request):
    ## do some magic.
    return False or True
is_appb_request = is_appa_request

def AppA(request):
    if is_appb_request(request):
       return AppB(request)
    ## Process AppA.
    return HttpResponseRedirect('/appa/thank_you/')

def AppB(request):
    if is_appa_request(request):
       return AppA(request)
    ## Process AppB.
    return HttpResponseRedirect('/appb/thank_you/')

This should yield a transparent experience for the end-user, and the client who hired you will likely never know the difference.

If you're not redirecting after the POST, aren't you worried about duplicate data due to the user refreshing the page?

Jack M.
It would be great if a simple solution like this worked. However, I need to display detailed results after processing the data. This would require sessions (as proposed by Matthew J Morrison) wouldn't it?
swisstony
You can do it one of three ways. #1, store the data in the database and pass the `pk` of the new entry when you redirect. #2, store the data in the `cache` backend, and pass the key again. #3, store it in the session. Any of these are perfectly normal for a web-app to do, even if it is temporary. If the form data is non-trivial to parse, it would also make the system faster if the output was already cached.
Jack M.
A: 

You need to use a HTTP 1.1 Temporary Redirect (307). Unfortunately, django redirect() and HTTPResponse(Permanent)Redirect return only a 301 or 302. (see the django.http module)

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