You cannot retrieve the context variables from a HttpResponseRedirect
. It doesn't make sense why you are setting the context variables if you are redirecting anyway.
You certainly should be able to pick up variables from session after redirecting. I have done this in several of my test cases. How are you asserting the session data in your test case?
This is how I go about asserting session variables after a redirect:
response = self.client.post(reverse('foo'))
self.assertRedirects(response, reverse('bar', args = ['baz']),
status_code = 302, target_status_code = 200)
self.assertEqual('value', self.client.session.get('key'))
Self.client
is an instance of django.test.client.Client
in this case.
Update
(In response to @Marconi's comment) Here is one way of displaying a message to the user after redirecting. This is copied almost verbatim from my answer to another question.
Your first view can create a message for the current using auth and have the second view read and delete it. Something like this:
def first_view(request, *args, **kwargs):
# all goes well
message = _("<message for user>")
request.user.message_set.create(message = message)
return redirect('second_view')
def second_view(request, *args, **kwargs):
# Render page
# Template for second_view:
{% for message in messages %}
...
{% endfor %}
Messages are saved to the database. This means that you can access them even after a redirect. They are automatically read and deleted on rendering the template. You will have to use RequestContext
for this to work.