python - 从 flask.session 读取返回一个空列表
问题描述
我的 Flask 应用程序具有以下路由,可在 WTForm 旁边呈现图像网格以供用户输入。我希望的行为是在提交表单但无效时保留当前的图像样本。我的想法是将来自最后一个 GET 请求的样本存储在 flask.session 代理中。但是,当从session
空列表中读取样本时会返回。
我已filesystem
在应用程序的配置中将会话类型设置为。我读过存储在 session 中的数据实际上是写入 cookie 的,因此需要是 JSON 可序列化的,但是由于我们正在处理字符串列表,所以这不是问题。
@app.route('/', methods=['GET', 'POST'])
@login_required
def hello():
input_form = TagForm()
data_index = g._index
if request.method == 'GET':
sample = sampling.sample(16, data_index)
print('storing sample')
session['curr_sample'] = sample
print('stored sample:')
print(session['curr_sample'])
elif request.method == 'POST':
print('reading sample')
sample = session['curr_sample']
print('stored sample:')
print(session['curr_sample'])
if input_form.validate_on_submit():
# commit tag to db
return redirect(url_for('hello'))
else:
flash('Could not commit.', 'danger')
return render_template('grid.html', sample=sample, form=input_form)
这是服务器的输出:
storing sample
stored sample:
['2_3_3.png', '2_11_15.png', '2_11_15.png', '2_14_15.png', '2_17_11.png', '2_11_11.png', '2_18_15.png', '2_10_0.png', '2_7_12.png', '2_3_2.png', '2_9_15.png', '2_19_12.png', '2_17_12.png', '2_5_11.png', '2_10_2.png', '2_11_11.png']
127.0.0.1 - - [07/Aug/2020 15:08:48] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2020 15:08:48] "GET /cdn/2_11_11.png HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2020 15:08:48] "GET /cdn/2_17_12.png HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2020 15:08:48] "GET /cdn/2_5_11.png HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2020 15:08:48] "GET /cdn/2_9_15.png HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2020 15:08:48] "GET /cdn/2_3_2.png HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2020 15:08:48] "GET /cdn/2_10_2.png HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2020 15:08:48] "GET /cdn/2_18_15.png HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2020 15:08:48] "GET /cdn/2_11_15.png HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2020 15:08:48] "GET /cdn/2_3_3.png HTTP/1.1" 200 -
reading sample
stored sample:
[]
127.0.0.1 - - [07/Aug/2020 15:09:40] "POST / HTTP/1.1" 302 -
解决方案
推荐阅读
- c# - 如何在属性中返回多个变量
- django - Django 无法投射日期时间
- apache-spark - Spark - 工人没有足够的资源
- google-cloud-platform - 列出 GCP 帐户/组织的所有用户
- python - 从文件构造结构化的numpy数组?
- ios - 声音在模拟器上播放,但在 iPhone 上不播放
- google-apps-script - 如何通过 Google Apps 脚本应用 GoogleSheet Filterview?
- android - 使用 Multipart 和 Retrofit2 的服务器上的文件显示已损坏
- javascript - Javascript window.open() 返回 null 并在选项卡中打开 - 仅限 Firefox
- ios - 如何以编程方式滚动到 tableViewHeader