Flask 中控制如何流回到 render_template 之前的语句?
How does control flow back to statements before render_template in flask?
我不清楚表单验证是如何在提交时发生的。当索引函数被调用时,if form.validate_on_submit
将
最初是假的吧?所以它应该 return 呈现的模板。但是只有对象 'form
' 被传递给渲染模板,所以在 return 渲染模板之后,控制将如何流回 if 语句? (if form.validate_on_submit
)
@app.route('/upload', methods=['GET', 'POST'])
def index():
if form.validate_on_submit():
blah blah blah I do something......
return redirect(url_for('index'))
return render_template('upload.html', form=form)
validate_on_submit
在 提交表单后使用。你的代码应该是这样的:
@app.route('/upload', methods=['GET', 'POST'])
def index():
form = MyForm()
if request.method == 'POST':
if form.validate_on_submit():
blah blah blah I do something......
return redirect(url_for('index'))
return render_template('upload.html', form=form)
不直接返回。它向浏览器发送表单并完成此功能(并且它忘记了当前客户端 - 它断开了该用户)。当用户在浏览器中单击表单中的按钮时,浏览器必须再次与服务器(烧瓶)连接,将新请求发送到 /upload
并将表单发送到服务器(烧瓶),服务器(烧瓶)获取表单作为新请求并运行 index()
再次。然后它再次运行if form.validate_on_submit
。
也许如果您必须学习 socket
并使用它从头开始构建服务器,那么您会看到它是如何工作的:)
我不清楚表单验证是如何在提交时发生的。当索引函数被调用时,if form.validate_on_submit
将
最初是假的吧?所以它应该 return 呈现的模板。但是只有对象 'form
' 被传递给渲染模板,所以在 return 渲染模板之后,控制将如何流回 if 语句? (if form.validate_on_submit
)
@app.route('/upload', methods=['GET', 'POST'])
def index():
if form.validate_on_submit():
blah blah blah I do something......
return redirect(url_for('index'))
return render_template('upload.html', form=form)
validate_on_submit
在 提交表单后使用。你的代码应该是这样的:
@app.route('/upload', methods=['GET', 'POST'])
def index():
form = MyForm()
if request.method == 'POST':
if form.validate_on_submit():
blah blah blah I do something......
return redirect(url_for('index'))
return render_template('upload.html', form=form)
不直接返回。它向浏览器发送表单并完成此功能(并且它忘记了当前客户端 - 它断开了该用户)。当用户在浏览器中单击表单中的按钮时,浏览器必须再次与服务器(烧瓶)连接,将新请求发送到 /upload
并将表单发送到服务器(烧瓶),服务器(烧瓶)获取表单作为新请求并运行 index()
再次。然后它再次运行if form.validate_on_submit
。
也许如果您必须学习 socket
并使用它从头开始构建服务器,那么您会看到它是如何工作的:)