Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete a session key in Django after it is used once?

I have two views.

view1 passes an error message to view2 through a session key.

How do I delete the key after view2 is rendered? I only need it for once: redirect from view1 to view2. I dont need that message to show up after refreshing my webpage. I don't think python will continue to execute once it reaches return

I was thinking about setting an expiration timestamp but I need to ensure that it exists for at least 10-20 seconds, if the application really does that long to load (we do some server stuff with Django)? So time is not that promising.

Thanks.

like image 576
CppLearner Avatar asked Mar 20 '12 04:03

CppLearner


People also ask

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.

Where is Django session stored?

Session data is stored in a database table named django_session . Django only sends a cookie if it needs to. If you don't set any session data, it won't send a session cookie.

How long do sessions last Django?

As you mentioned in your question, sessions in Django live for as long as SESSION_COOKIE_AGE determines (which defaults to 2 weeks) from the last time it was "accessed". Two exceptions for that: you can set an expiry time to a session yourself, and then it depends on that.

How does Django keep track of a session?

Django uses a cookie containing a special session id to identify each browser and its associated session with the site. The actual session data is stored in the site database by default (this is more secure than storing the data in a cookie, where they are more vulnerable to malicious users).


1 Answers

You can delete the key from the session like any other dictionary.

del request.session['your key'] 

You may need to mark the session as modified for it to save, depending on some of your settings.

request.session.modified = True 
like image 130
5 revs, 4 users 78% Avatar answered Sep 28 '22 00:09

5 revs, 4 users 78%