do sessions work when redirecting?

551 views
Skip to first unread message

Mike Chambers

unread,
May 13, 2008, 3:23:17 PM5/13/08
to django...@googlegroups.com
I am running into an issue where my session values are not remembered if
I do an HTTP redirect.

If I do:

--
request.session['a'] = 'aaaa'
return HttpResponseRedirect(request.META['HTTP_REFERER']
--

The session variable a will not be available once the browser is redirected.

If I do:

--
request.session['a'] = 'aaaa'
render_to_response(...)
--

Then the session is available as expected.

I have tried to set:

request.session.modified = True

but that does not change the behavior.

Can session variables be set when doing a redirect?


mike

Rajesh Dhawan

unread,
May 13, 2008, 5:12:32 PM5/13/08
to Django users
Absolutely.

You should ensure that request.META['HTTP_REFERER'] doesn't redirect
to a different domain name that happens to map to the same
application. For example, if you set the session in a view at
http://127.0.0.1/myview/ and that view redirects to http://localhost/myview2/,
you will not be able to access the previous session.

Mike Chambers

unread,
May 13, 2008, 5:50:19 PM5/13/08
to django...@googlegroups.com
Thanks for the input.

I am narrowing down the issue. Basically, If i try to place a copy of
the request.POST data into the session, nothing will get stored:

i.e.

--
post_copy = request.POST.copy()
request.session['form_post_data'] = post_copy
return HttpResponseRedirect(request.META['HTTP_REFERER'])
--

After this

request.session.keys() returns [] (an empty list)

if I do:

post_copy = request.POST.copy()
request.session['form_post_data'] = post_copy
print request.session.get('form_post_data')
return HttpResponseRedirect(request.META['HTTP_REFERER'])

this outputs:
--
<QueryDict: {u'comment': [u''], u'user_name': [u''], u'user_email':
[u''], u'user_url': [u'']}>
--

However, it it still not actually saved and available during a seperate
request.

mike

Mike Chambers

unread,
May 13, 2008, 6:04:09 PM5/13/08
to django...@googlegroups.com
I ended up getting this to work with (what feels like) a hack:


To save the session:

--
request.session['form_post_query_string'] = request.POST.urlencode()


return HttpResponseRedirect(request.META['HTTP_REFERER'])
--


then to later access it and use it:

--
from django.http import QueryDict

q = QueryDict(request.session['form_post_query_string'])
comment_form = CommentForm(q)
--

Anyone know why I cant store request.POST.copy() in the session?

Is that a bug?

mike

Mike Chambers

unread,
May 13, 2008, 7:50:42 PM5/13/08
to django...@googlegroups.com
ok. more info on this. (Sorry about all of the emails, but I am really
trying to track this down, and see if it is a bug).

If I try to include the request.POST data QueryDict in the session, then
no session data is saved between requests (i.e. it wipes other session
data):


request.session['form_post_data'] = request.POST.copy()
request.session['foo'] = "bar"

Then, in another request:

print request.session.keys()

prints []

But:

request.session['foo'] = "bar"

then in another request:

print request.session.keys()

prints ['foo']

So, at this point, Im thinking it is a bug.

mike

Juanjo Conti

unread,
May 13, 2008, 8:20:24 PM5/13/08
to django...@googlegroups.com
On Tue, May 13, 2008 at 7:04 PM, Mike Chambers <mikech...@gmail.com> wrote:
>
> Anyone know why I cant store request.POST.copy() in the session?

Not sure way, but I save it as a regular dict like this:

request.session['POST'] = dict(request.POST.items())

Greets!
--
Juanjo Conti

Mike Chambers

unread,
May 14, 2008, 1:21:02 AM5/14/08
to django...@googlegroups.com
fyi, bug logged here with test case:

http://code.djangoproject.com/ticket/7233

mike

Rajesh Dhawan

unread,
May 14, 2008, 2:55:01 PM5/14/08
to Django users
Hi Mike,

On May 14, 1:21 am, Mike Chambers <mikechamb...@gmail.com> wrote:
> fyi, bug logged here with test case:
>
> http://code.djangoproject.com/ticket/7233

I've just uploaded a patch to that ticket with an explanation.

Hopefully, you can test that.

-Rajesh
Reply all
Reply to author
Forward
0 new messages