使用下面的应用程序和 Flask 0.11.1,我导航到与以下函数调用关联的路由,并得到给定的结果:
- create(): '1,2,3' # 确定
- 删除(1) : '2,3' # 确定
- remove(2) : '1,3' # 预期为 '3'
- maintain(): '1,2,3' # 预期为 '1,3' 或 '3'
from flask import Flask, session
app = Flask(__name__)
@app.route('/')
def create():
session['list'] = ['1','2','3']
return ",".join(session['list'])
@app.route('/m')
def maintain():
return ",".join(session['list'])
@app.route('/r/<int:id>')
def remove(id):
session['list'].remove(str(id))
return ",".join(session['list'])
if __name__ == '__main__':
app.secret_key = "123"
app.run()
这个问题的主题类似于this question , this , 和 this one ,但我设置的是 key 而不是重新生成它,而且我的变量肯定不会大于 cookie 允许的 4096 字节。也许我缺少对 Flask session 变量的一些更基本的了解?
最佳答案
来自 the doc :
Be advised that modifications on mutable structures are not picked up automatically, in that situation you have to explicitly set the [
modified
attribute] toTrue
yourself.
尝试:
session['list'].remove(str(id))
session.modified = True
关于python - Flask session 变量在请求之间不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39261260/