2016-02-24 32 views
1

後,我有這樣的代碼:瓶會話是空的重定向

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']存儲文件內容,如預期的那樣。

有人可以建議這裏有什麼問題嗎?瓶文檔和谷歌搜索沒有幫助:(

回答

3

望着sessions implementation,似乎剛剛瓶所有會話數據保存到該cookie。

和最大的cookie大小,根據這個answer,是4KB如果你的文件比則瀏覽器大可以直接拒絕該cookie。

在任何情況下,文件存儲到會話看起來並不像一個好主意。

+0

這麼簡單...謝謝! –

+0

請問如何解決這個問題? – hubert

+0

@hubert如果你有上述問題(會話數據太大而無法存儲在cookie中),那麼您需要更改代碼以減小數據量 - 不要將大的東西存儲到cookie中,或使用瀏覽器本地存儲來保存用戶數據而不是cookie(或兩者都像在cookie中擁有用戶身份信息一樣,因此您可以識別服務器上的用戶並將用戶數據緩存到本地存儲中)。 –