我的玩具应用程序的查看功能是:
@app.route('/', methods=['GET', 'POST'])
def index():
name = None
form = NameForm()
if form.validate_on_submit():
name = form.name.data
form.name.data = ''
return render_template('index.html', form=form, name=name)
当我使用PRG时它看起来像这样:
@app.route('/', methods=['GET', 'POST'])
def index():
form = NameForm()
if form.validate_on_submit():
session['name'] = form.name.data
return redirect(url_for('index'))
return render_template('index.html', form=form, name=session.get('name'))
如您所见,form.name.data = ''
行用于清除第一个版本中的输入字段,但在第二个版本中不需要它。我认为Flask-WTF会自动将StringField
中的文本传递给新的form
实例,但由于某些原因,它没有。
我的问题是:当我使用PRG时,为什么form.name.data
在不同请求之间不再可用?
答案 0 :(得分:1)
它无法在重定向上传递任何内容,因为它是一个全新的请求。