我有这样的代码:
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']
按预期存储文件内容。在
有人能提出问题吗?烧瓶医生和谷歌也没用
看看sessions implementation,flask似乎只是将所有会话数据保存到cookie中。在
根据这个answer,最大cookie大小是4KB。如果你的文件比这个大,那么浏览器可以拒绝cookie。在
无论如何,将文件存储到会话中看起来不是一个好主意。在
相关问题 更多 >
编程相关推荐