问题
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.
回答1:
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
回答2:
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')
来源:https://stackoverflow.com/questions/9781201/how-do-i-delete-a-session-key-in-django-after-it-is-used-once