在Django会话中修改字典不会修改会话 [英] Modifying Dictionary in Django Session Does Not Modify Session
本文介绍了在Django会话中修改字典不会修改会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
>>> request.session ['my_dict'] = {'a':1,'b':2,'c':3}
我遇到的问题是,当我直接修改字典时,下一个请求中的值不会被更改:
>>> request.session ['my_dict']。pop('c')
3
>>> request.session.has_key('c')
False
#看起来不错...
...
#下一个请求
>>> request.session.has_key('c')
True
#什么给!
解决方案
SESSION_SAVE_EVERY_REQUEST = True
这将使每一个请求都发生这种情况。如果你的代码发生了很多,可能值得吗?我猜,偶尔的额外开销不会太多,远远低于从忽视的潜在问题,包括
request.session.modified = True
每次。
I am storing dictionaries in my session referenced by a string key:
>>> request.session['my_dict'] = {'a': 1, 'b': 2, 'c': 3}
The problem I encountered was that when I modified the dictionary directly, the value would not be changed during the next request:
>>> request.session['my_dict'].pop('c')
3
>>> request.session.has_key('c')
False
# looks okay...
...
# Next request
>>> request.session.has_key('c')
True
# what gives!
解决方案
As the documentation states, another option is to use
SESSION_SAVE_EVERY_REQUEST=True
which will make this happen every request anyway. Might be worth it if this happens a lot in your code; I'm guessing the occasional additional overhead wouldn't be much and it is far less than the potential problems from neglecting from including the
request.session.modified = True
line each time.
这篇关于在Django会话中修改字典不会修改会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文