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.
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.
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.
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.
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).
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
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