python - Flask session 变量在请求之间不持久

标签 python session flask

使用下面的应用程序和 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] to True yourself.

尝试:

session['list'].remove(str(id))
session.modified = True

关于python - Flask session 变量在请求之间不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39261260/

相关文章:

python - python中的分段线性回归

javascript - 在 session ID 上下文中使用 javascript 登录

performance - 我可以使用 Grails session 来存储整个域对象吗?

python - 导入错误 : No module named flask. 分机存储

authentication - Flask 安全登录和注销

python - 在任何 3 维表面上生成随机点

python - 在 Python 中检测图形驱动程序信息

python - Flask-login 如何识别单个 session 登录的不同用户?

路径中的 Python Azure 应用程序 404

python - 在 python 命令行中多次使用同一选项