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
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
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
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
Not sure way, but I save it as a regular dict like this:
request.session['POST'] = dict(request.POST.items())
Greets!
--
Juanjo Conti