CppLearner
CppLearner

Reputation: 17040

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.

Upvotes: 41

Views: 50676

Answers (3)

user13681264
user13681264

Reputation: 21

if "uid" in self.request.session.keys():
    del self.request.session["uid"]

Upvotes: 2

Jonathan
Jonathan

Reputation: 2653

You could also pop the key from the session. You could set the key to a variable and get rid of it at the same time:

key_variable = request.session.pop('your key')

Upvotes: 30

Collin Green
Collin Green

Reputation: 2196

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

Upvotes: 74

Related Questions