繁体   English   中英

使用Flask和WTForms链接表单的问题

[英]Problem with linking forms using Flask and WTForms

我正在尝试建立一个可运行基于用户输入方法的Web应用程序。 第一页获取图像和图像数量,并运行图像方法,然后将用户发送到下一个表单,该表单是确认按钮,确认他们已完成应用程序外部的任务。 但是,当按下确认按钮时,用户将被重定向到第一页,该页面显示即使先前已经填写了信息,也需要他们将信息输入到表单中

@app.route("/", methods=['GET', 'POST'])
def Home():
    form = SelectImageForm()
    if form.validate_on_submit():
        Label_Required = form.ImageLabel.data
        Amount_Required = form.Amount.data
        Cloud_Transfer(form.ImageLabel.data, form.Amount.data)
        flash(f'Searching Labels for {form.ImageLabel.data}!', 'success')
        form = SelectXMLConversion()
        return Stage2()
    return render_template('home.html', title = 'Label Selection', form=form )

@app.route('/stage2', methods=['GET','POST'])
def Stage2():
    form =  SelectXMLConversion()
    if form.validate_on_submit():
        return render_template('stage2.html', title ='Label Selection', form=form)


if __name__ == '__main__':
    app.run(debug='true')

目前,它只是希望重新加载第2阶段的页面,但它会将它们发送回首页,提示他们填写必填字段

不要return Stage2()

from flask import redirect, url_for

def home():
form = SelectImageForm()
if form.validate_on_submit():
    Label_Required = form.ImageLabel.data
    Amount_Required = form.Amount.data
    Cloud_Transfer(form.ImageLabel.data, form.Amount.data)
    flash(f'Searching Labels for {form.ImageLabel.data}!', 'success')
    form = SelectXMLConversion()
    return redirect(url_for('stage2'))
return render_template('home.html', title = 'Label Selection', form=form )

同样,路由定义应该是函数,应该像def home()def stage2()一样是小写字母。 我相信label_requiredamount_required也应该是变量,也应该是小写。 您可能需要检查Python命名约定

最后, stage2()原样, stage2()路由将失败。 除非表单经过验证,否则您不会返回模板。 据推测,你希望人们填写此航线上首次的形式,在这种情况下,你必须有一个return的那之外if form.validate_on_submit():条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM