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

70👍

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

30👍

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

2👍

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

Leave a comment