53

I have a form that redirects to the same page after a user enters information (so that they can continue entering information). If the form submission is successful, I'm returning

HttpResponseRedirect(request.path)

which works fine. However, I'd also like to display some messages to the user in this case (e.g., "Your data has been saved" at the top of the screen). If I weren't redirecting, I'd just return these messages in the context dictionary. With the redirect, however, I can't do this.

So how can I pass template context information when using HttpResponseRedirect?

What I'm trying to do seems like it would be incredibly common, so please excuse me if I'm missing something obvious.

Jeff
  • 14,831
  • 15
  • 49
  • 59
  • It seems the loading of the second page takes place after another separate request from the browser (the first response returns a 302) so you can't pass information back and forth between views like you might expect using HttpResponseRedirect. IMHO, the session framework is the easiest way to pass information between requests. – Hartley Brody Mar 03 '12 at 03:04

7 Answers7

75

For the sake of completion and future reference, you can now use the messages framework. After you install it:

views.py

from django.contrib import messages

def view(request):
  # your code
  messages.success(request, "Your data has been saved!")
  HttpResponseRedirect(request.path)

template.html

{% if messages %}
<ul class="messages">
  {% for message in messages %}
  <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
  {% endfor %}
</ul>
{% endif %}
João Pesce
  • 2,424
  • 1
  • 24
  • 26
  • Currently ["The default settings.py created by django-admin startproject already contains all the settings required to enable message functionality"](https://docs.djangoproject.com/en/dev/ref/contrib/messages/), so just `from django.contrib import messages` – Vladislav Povorozniuc Jan 19 '23 at 21:09
13

if you are using auth and have a logged in user you could:

http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.User.message_set.create

GET params are also hackable. The querystring, as mentioned in other answers, could be used.

I think the most preferred way would be to use the sessions framework. That way you can load up whatever you want in the context and get

{{ request.session.foo }} 

foo could be the message or you could do:

{% ifequal request.session.foo 1 %} Nice work! {% else %} Almost! {% endifequal %}

and other fun stuff.

http://docs.djangoproject.com/en/dev/topics/http/sessions/#using-sessions-in-views

Anupam
  • 14,950
  • 19
  • 67
  • 94
Skylar Saveland
  • 11,116
  • 9
  • 75
  • 91
8

You can't. HttpResponseRedirect sends a client-side redirect (HTTP status code 302) to the browser, and then the browser re-requests another page.

You can set a URL query string on the redirect, though that will be visible to the user and anyone intercepting HTTP requests (i.e. proxies), and is therefore not suitable for sensitive information.

dcrosta
  • 26,009
  • 8
  • 71
  • 83
7

The best way would probably be to use a coded querystring on the redirect URL... its an old school approach.

You could do something like

/page/?m=1, /page/?m=2, etc

You would then parse that variable with request.GET in the view code and show the appropriate message.

M. Ryan
  • 6,973
  • 11
  • 52
  • 76
6

From your views.py you hast have to put a key/value-pair into the session and then read it from the HTML template.

For example:

views.py

# your code here
request.session['vote'] = 1
return HttpResponseRedirect(request.path)

your_template.html

{% ifequal request.session.vote 1 %}
    <!-- Your action here -->
{% endifequal  %}
Benny Code
  • 51,456
  • 28
  • 233
  • 198
  • An old answer, but I suspect something like this could be used for the question I asked here: http://stackoverflow.com/questions/41317128/using-httpresponseredirect-in-a-django-view-where-context-has-to-be-passed Would love to hear your thoughts on it if you've got time :-) – Hassan Baig Dec 24 '16 at 22:35
1

The only way I know of to pass any data with a redirect is to add GET parameters to the URL you're passing in. To avoid XSS hacks you'd want to pass a specific constant like:

[current path you're passing in]?message=saved

And then process the message=saved parameter in the handler for the path you passed in.

A somewhat more complicated way would be not passing the data in the redirect, and instead using something like http://code.google.com/p/django-notify/ to store session-based data that is displayed to the user following the redirect.

Ben Regenspan
  • 10,058
  • 2
  • 33
  • 44
0

You add ?saved=1 to the query string and check for it with something like:

saved = request.GET.get('saved', False)
Jeff Ober
  • 4,967
  • 20
  • 15