Pav*_*dhu 2 python session flask
我想在 Flask 中的视图之间传递一个大列表,但是我不能把它放在一个会话中,因为它超过了 4096 字节的限制。有没有办法通过像这样的表单在页面之间传递列表?
Python:
@app.route('/send')
def send():
list = ['item1', 'item2', 'item3', 'item4']
return render_template('send.html', list=list)
@app.route('/receive', methods=['POST', 'GET'])
def receive():
list = request.form['list']
return render_template('receive.html')
Run Code Online (Sandbox Code Playgroud)
发送.html:
<form method="POST" action="{{ url_for('receive') }}">
<input type="text" name="list" value="{{ list }}">
<submit>Submit</submit>
</form>
Run Code Online (Sandbox Code Playgroud)
这行得通吗?谢谢。
小智 5
我有一个类似的问题,在视图之间传递一个大列表。这是我所做的:
import tempfile
import pickle
import shutil
mylist = ['a', 'b', 'c']
session['tempdir'] = tempfile.mkdtemp()
outfile = open(session['tempdir'] + '/filename', 'wb')
pickle.dump(mylist, outfile)
outfile.close()
Run Code Online (Sandbox Code Playgroud)
要在另一个视图中检索列表:
infile = open(session['tempdir'] + '/filename', 'rb')
mylist = pickle.load(infile)
infile.close()
Run Code Online (Sandbox Code Playgroud)
然后记得在完成后删除您的临时目录、文件和清除会话:
shutil.rmtree(session['tempdir'])
session.pop('tempdir', None)
Run Code Online (Sandbox Code Playgroud)