重拨后Flask会话为空

2024-10-02 02:44:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的代码:

from flask import Flask, render_template, redirect, request, url_for, session

app = Flask(__name__)


@app.route('/')
def index():
    tmplt = session.get('template', 'No template')
    return render_template('index.html', template=tmplt.decode('utf-8'))


@app.route('/template', methods=['POST'])
def upload_template():
    session['template'] = request.files['template'].read()
    return redirect(url_for('index'))


if __name__ == '__main__':
    app.secret_key = '\x0cw1\xd4\xd5\x80%O?q \xcfQrrk\xa3H\xc0[J\xae<\xc3]\xe6\x10\xc0-\xf8S\x08P4[3]PrK\xa9\xf1'
    app.run(debug=True)

我希望在成功执行POST /template之后,tmplt变量将等于上载的值。然而,它是空的。调试显示重定向之前的session['template']按预期存储文件内容。在

有人能提出问题吗?烧瓶医生和谷歌也没用


Tags: nameappurlflaskforindexreturnrequest
1条回答
网友
1楼 · 发布于 2024-10-02 02:44:38

看看sessions implementation,flask似乎只是将所有会话数据保存到cookie中。在

根据这个answer,最大cookie大小是4KB。如果你的文件比这个大,那么浏览器可以拒绝cookie。在

无论如何,将文件存储到会话中看起来不是一个好主意。在

相关问题 更多 >

    热门问题