I have a view that looks like this:
def login(request):
...
# some variables has been set here
...
if request.POST.get('remember_me', None):
request.session.set_expiry(1209600) # 2 weeks
return HttpResponseRedirect(reverse('dashboard'))
Now If I assert for the variables using context, I get the error: “AttributeError: ‘HttpResponseRedirect’ object has no attribute ‘context’ ” this is the same for sessions. How do I test it then that some variables has been set and possible session expiration has been set even if i redirect?
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:
Self.clientis an instance ofdjango.test.client.Clientin 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:
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
RequestContextfor this to work.