简体   繁体   中英

Flask session is empty after redirect

I have such code:

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)

I expect, that after successful execution of POST /template , variable tmplt will be equal to what was upload. However, it is empty. Debugging shows that session['template'] before redirection stores file content, as expected.

Anybody can suggest what's the problem here? Flask docs and googling didn't help :(

Looking at the sessions implementation , it seems that flask just saves all the session data into the cookie.

And the maximum cookie size, according to this answer , is 4KB. If your file is larger than that then browser can just reject the cookie.

In the any case, storing the file into the session doesn't look like a good idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM