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

后端 未结 3 1660
离开以前
离开以前 2020-12-15 17:13

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:

相关标签:
3条回答
  • 2020-12-15 18:04

    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')
    
    0 讨论(0)
  • 2020-12-15 18:07

    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
    
    0 讨论(0)
  • 2020-12-15 18:08
    if "uid" in self.request.session.keys():
        del self.request.session["uid"]
    
    0 讨论(0)
提交回复
热议问题