In one view, I set:
request.session.set_expiry(999)
request.session['test'] = '123'
In another view, I do:
print request.session['test']
and it cannot be found. (error) It's very simple, I just have 2 views.
It seems that once I leave a view and come back to it...it's gone! Why?
This is how sessions work: The server then sends a cookie named sessionid , containing the session key, as value to the browser. On subsequent requests, the browser sends the cookie sessionid to the server. Django then uses this cookie to retrieve session data and makes it accessible in your code.
If you want to use a database-backed session, you need to add 'django. contrib. sessions' to your INSTALLED_APPS setting. Once you have configured your installation, run manage.py migrate to install the single database table that stores session data.
To delete a session or any particular key of that session, we can use del. The output will look like this and don't worry if your cookie didn't delete because we use this method only to delete your data in the Django database and not the session ID and cookie itself.
Could it be related to this?, just found it at http://code.djangoproject.com/wiki/NewbieMistakes
Appending to a list in session doesn't work Problem
If you have a list in your session, append operations don't get saved to the object. Solution
Copy the list out of the session object, append to it, then copy it back in:
sessionlist = request.session['my_list']
sessionlist.append(new_object)
request.session['my_list'] = sessionlist
Are you, by any chance, setting the session itself to an empty dictionary, somewhere?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With