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

只愿长相守 提交于 2019-11-30 14:35:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!