Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django, my request.session is not carrying over...does anyone know why?

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?

like image 568
TIMEX Avatar asked Feb 04 '10 11:02

TIMEX


People also ask

How does request session work in Django?

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.

How does Django manage user sessions?

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.

How do I stop a session in Django?

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.


2 Answers

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
like image 80
Jesus Benito Avatar answered Oct 15 '22 12:10

Jesus Benito


Are you, by any chance, setting the session itself to an empty dictionary, somewhere?

like image 36
lprsd Avatar answered Oct 15 '22 13:10

lprsd