Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a session variable in django?

Tags:

python

django

my session variable is 'cart':

cart = {'8': ['a'], ['b'], '9': ['c'], ['d']}

if I want to delete all my cart variable I just do this in Python:

del request.session['cart']

but I just want to delete key '8', so I try this but it doesn't work:

del request.session['cart']['8']

however if I print request.session['cart']['8'] and get a, b

like image 829
ezdookie Avatar asked Aug 12 '14 21:08

ezdookie


People also ask

How do you remove a session variable in Python?

To remove a session variable, use the pop() method on the session object and mention the variable to be removed.

How do you delete 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.

How do you delete the session data?

Remove a particular attribute − You can call the public void removeAttribute(String name) method to delete the value associated with the particular key. Delete the whole session − You can call the public void to invalidate() method to discard an entire session.

How do I reset a session flask?

There is no way to clear session or anything. One must simply change the app. config["SECRET_KEY"] and the contents in session dictionary will get erased.


2 Answers

The django session object can only save when its modified. But because you are modifying an object within session, the session object doesn't know its being modified and hence it cant save.

To let the session object know its modified use:

request.session.modified = True

From the django docs :

https://docs.djangoproject.com/en/dev/topics/http/sessions/

When sessions are saved By default, Django only saves to the session database when the session has been modified – that is if any of its dictionary values have been assigned or deleted:

# Session is modified. 
request.session['foo'] = 'bar'

# Session is modified. 
del request.session['foo']

# Session is modified. 
request.session['foo'] = {}

# Gotcha: Session is NOT modified, because this alters
# request.session['foo'] instead of request.session.
request.session['foo']['bar'] = 'baz' 

In the last case of the above example, we can tell the session object explicitly that it has been modified by setting the modified attribute on the session object:

  request.session.modified = True

To change this default behavior, set the SESSION_SAVE_EVERY_REQUEST setting to True. When set to True, Django will save the session to the database on every single request.

Note that the session cookie is only sent when a session has been created or modified. If SESSION_SAVE_EVERY_REQUEST is True, the session cookie will be sent on every request.

Similarly, the expires part of a session cookie is updated each time the session cookie is sent.

The session is not saved if the response’s status code is 500.

like image 194
agconti Avatar answered Sep 18 '22 18:09

agconti


My request.session['cart'] was a list, like this: [u'2', u'2', u'1']

So this worked for me:

list_cart  = request.session['cart']
list_cart.remove('2')

result: [u'2', u'1']

like image 41
Jessica O Avatar answered Sep 18 '22 18:09

Jessica O