简体   繁体   English

烧瓶 wtform 验证失败

[英]flask wtform validation fails

class FlowerForm(FlaskForm):
    cr_score = IntegerField('Credit Score', validators=[DataRequired()])
    geo = StringField('Geography', validators=[DataRequired()])
    gen = StringField('Gender', validators=[DataRequired()])
    age = IntegerField('Age', validators=[DataRequired()])
    ten = IntegerField('Tenure', validators=[DataRequired()])
    bal = IntegerField('Balance', validators=[DataRequired()])
    num = IntegerField('Number Of Products', validators=[DataRequired()])
    has_card = IntegerField('Has Credit Card', validators=[DataRequired()])
    is_active = IntegerField('Is Active Member', validators=[DataRequired()])
    sal = IntegerField('Estimated Salary', validators=[DataRequired()])
    
    submit = SubmitField('Analyze')

@app.route('/', methods=['GET', 'POST'])
def index():
    # Create instance of the form.
    form = FlowerForm(request.form)
    # If the form is valid on submission
    if request.method == "POST" and form.validate_on_submit():
   
        # Grab the data from the input on the form.
        session["cr_score"] = form.cr_score.data
        session["geo"] = form.geo.data
        session["gen"] = form.gen.data
        session["age"] = form.age.data
        session["ten"] = form.ten.data
        session["bal"] = form.bal.data
        session["num"] = form.num.data
        session["has_card"] = form.has_card.data
        session["is_active"] = form.is_active.data
        session["sal"] = form.sal.data
        
        return redirect(url_for("prediction"))
    print(form.errors)
    return render_template('home.html', form=form)

@app.route('/prediction')
def prediction():
    #Defining content dictionary
    content = {}
    
    content["CreditScore"] = int(session["cr_score"])
    content["Geography"] = str(session["geo"])
    content["Gender"] = str(session["gen"])
    content["Age"] = int(session["age"])
    content["Tenure"] = int(session["ten"])
    content["Balance"] = int(session["bal"])
    content["NumOfProducts"] = int(session["num"])
    content["HasCrCard"] = int(session["has_card"])
    content["IsActiveMember"] = int(session["is_active"])
    content["EstimatedSalary"] = int(session["sal"])


    results = return_prediction(model = model, scaler = scaler, onehot = ohencoder, lab_enc=labencoder, sample_json = content)
    return render_template('prediction.html',results=results)

The form validation does not seem to work here.表单验证在这里似乎不起作用。 I've tried form.valid_on_submit too.我也试过form.valid_on_submit

My home.html file and prediction.html file both have form.hidden_tag() in it.我的 home.html 文件和 prediction.html 文件都包含form.hidden_tag()

The if statement gets overriden somehow and I end up back on the home page if 语句以某种方式被覆盖,我最终回到主页

HTML home.html code: HTML home.html代码:

<h1>Welcome to Customer Retention Prediction</h1>
<h2>Please enter customer details below:</h2>
<form action = "/" method=”POST”>
 {# This hidden_tag is a CSRF security feature. #}
 {{form.hidden_tag()}}
 {{ form.csrf_token }}
 {{form.cr_score.label}} {{form.cr_score}}
 <br>
 {{form.geo.label}} {{form.geo}}
 <br>
 {{form.gen.label}} {{form.gen}}
 <br>
 {{form.age.label}} {{form.age}}
 <br>
 {{form.ten.label}} {{form.ten}}
 <br>
 {{form.bal.label}} {{form.bal}}
 <br>
 {{form.num.label}} {{form.num}}
 <br>
 {{form.has_card.label}}{{form.has_card}}
 <br>
 {{form.is_active.label}} {{form.is_active}}
 <br>
 {{form.sal.label}} {{form.sal}}
 <br>
 {{form.submit()}}
</form>

prediction.html code: prediction.html代码:

<h1>Thank You.</h1>
 {# This hidden_tag is a CSRF security feature. #}
 {{form.hidden_tag()}}
<h2>Probability of this customer leaving the bank is: {{results}}</h2>

Have you tried printing the error After the if statement try print(form.errors) , it should give you error no crsf token .您是否尝试过在 if 语句 try print(form.errors)之后打印错误,它应该给您错误no crsf token

In html add this where you used form.hidden_tag() Below it use {{ form.csrf_token }}在 html 中添加这个你使用form.hidden_tag()在它下面使用{{ form.csrf_token }}

[...]
if request.method == "POST" and form.validate():
        # Grab the data from the input on the form.
        session["cr_score"] = form.cr_score.data
        session["geo"] = form.geo.data
        session["gen"] = form.gen.data
        session["age"] = form.age.data
        [....]
print(form.errors)
return render_template('home.html', form=form)

Edit: you are not using action in form tag.编辑:您没有在表单标签中使用操作。

In html file: it should be this在html文件中:应该是这个

<form action="/" method="POST">

your index function will be:您的索引功能将是:

@app.route('/', methods=['GET', 'POST'])
def index():
    # Create instance of the form.
    form = FlowerForm(request.form)
    # If the form is valid on submission
    if request.method == "POST":
        print("yoyo")
        if form.validate_on_submit():
            
            # Grab the data from the input on the form.
            session["cr_score"] = form.cr_score.data
            session["geo"] = form.geo.data
            session["gen"] = form.gen.data
            session["age"] = form.age.data
            session["ten"] = form.ten.data
            session["bal"] = form.bal.data
            session["num"] = form.num.data
            session["has_card"] = form.has_card.data
            session["is_active"] = form.is_active.data
            session["sal"] = form.sal.data
            
            return redirect(url_for("prediction"))
    print(form.errors)
    return render_template('home.html', form=form)

Your home.html will be:您的 home.html 将是:

<h1>Welcome to Customer Retention Prediction</h1>
<h2>Please enter customer details below:</h2>
<form action="/" method="POST">
 {# This hidden_tag is a CSRF security feature. #}
 {{form.hidden_tag()}}

 {{ form.csrf_token }}
 {{form.cr_score.label}} {{form.cr_score}}
 <br>
 {{form.geo.label}} {{form.geo}}
 <br>
 {{form.gen.label}} {{form.gen}}
 <br>
 {{form.age.label}} {{form.age}}
 <br>
 {{form.ten.label}} {{form.ten}}
 <br>
 {{form.bal.label}} {{form.bal}}
 <br>
 {{form.num.label}} {{form.num}}
 <br>
 {{form.has_card.label}}{{form.has_card}}
 <br>
 {{form.is_active.label}} {{form.is_active}}
 <br>
 {{form.sal.label}} {{form.sal}}
 <br>
 {{form.submit()}}
  {{form.errors}}
</form>

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

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