Home > Software engineering >  validate_on_submit() not working in Flask. What should I do?
validate_on_submit() not working in Flask. What should I do?

Time:10-07

I am new to Flask.

The validate_on_submit() is not working and I also don't know what app.app_context() and app.test_request_context() do in my code.

The only thing I am trying to do is get my form to validate I can't figure out why it's not working.

This is my main.py

from flask import Flask, render_template, request, flash
from the_first_wt_form import MyForm

app = Flask(__name__)
app.config['SECRET_KEY'] = '934kegn298u54kjgnjdkrbg9u939'


with app.app_context():
    with app.test_request_context():
        a_form = MyForm()


@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == "POST":
        name = request.form['name']
        print(name)
        email = request.form['email']
        print(email)
        passwrd = request.form['password']
        print(passwrd)
        con = request.form['confirm']
        print(con)
        if a_form.validate_on_submit():
            print("Good job")
            name = request.name.data
            print(name)
        else:
            print('We messed up')

        if a_form.errors != {}:
            for err in a_form.errors.values():
                print(f"There was an error with creating user: {err}")
                flash(f"There was an error with creating user: {err}", category='danger')
    return render_template('mynewhome.html', form=a_form)

if __name__ == "__main__":
    app.run(debug=True)

This is the code from My wt_form.py

from wtforms import StringField, PasswordField, validators, SubmitField 

from flask_wtf import FlaskForm



class MyForm(FlaskForm):
    name = StringField('name', [validators.Length(min=4, max=25), validators.DataRequired()])
    email = StringField('Email Address', [validators.Length(min=6, max=35), validators.Email()])
    password = PasswordField('New Password', [
        validators.DataRequired(),
        validators.EqualTo('confirm', message='Passwords must match')
    ])
    confirm = PasswordField('Repeat Password')
    submit = SubmitField('Register')

And Finally this is mynewhome.html

<!DOCTYPE html> <html lang="en"> <head>
    <meta charset="UTF-8">
    <title>How are you?</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-uWxY/CJNBR 1zjPWmfnSnVxwRheevXITnMqoEIeG1LJrdI0GlVs/9cVSyPYXdcSF" crossorigin="anonymous">

</head> <body>

<h1> Hello BRo </h1> <br><br> {% with messages = get_flashed_messages(with_categories = true) %}
    {% if messages %}
        {% for category, message in messages %}
            <div class="alert alert--{{ category }}">
                <button type="button" class="m1-2 mb-1 close" data-dismiss="alert" aria-label="Close">
                    {{ message }}
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        {% endfor %}
    {% endif %}

{% endwith %} <br><br>

<div class="container">
    <form method="POST" action="/" class="form-register">
        {{ form.hidden_tag() }}
        {{ form.name.label }} {{ form.name(class = "form-control", Placeholder = "Usern Name") }}
        {{ form.email.label }} {{ form.email(class = "form-control", Placeholder = "Email Address") }}
        {{ form.password.label }} {{ form.password(class = "form-control", Placeholder = "Password") }}
        {{ form.confirm.label }} {{ form.confirm(class = "form-control", Placeholder = "Confirm Password") }}
        <br>
         {{ form.submit(class = "btn btn-lg btn-block btn-primary") }}
    </form> </div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kQtW33rZJAHjgefvhyyzcGF3C5TFyBQBA13V1RKPf4uH bwyzQxZ6CmMZHmNBEfJ" crossorigin="anonymous"></script> </body> </html>

CodePudding user response:

As a new flask user and simple flask usage, you should not need app_context and test_request_context. You can have a look on the documentation to understand them, but you don't need them in this situation.

You have to instantiate your form in the view function home

It is also a better practice to use form data only after validation, as you never know what the user enter in your form.

In the import you were importing the_first_wt_form but you said your file is called wt_form so I made the appropriate changes. But accordingly to your modules setup it might be wrong.

The main.py should look like (I tested it):

from flask import Flask, render_template, request, flash
from wt_form import MyForm

app = Flask(__name__)
app.config['SECRET_KEY'] = '934kegn298u54kjgnjdkrbg9u939'


@app.route('/', methods=['GET', 'POST'])
def home():
    a_form = MyForm()
    if request.method == "POST":
        if a_form.validate_on_submit():
            print("Good job")
            name = a_form.name.data
            print(name)
            # (...)
        else:
            print('We messed up')

            if a_form.errors != {}:
                for err in a_form.errors.values():
                    print(f"There was an error with creating user: {err}")    
                    flash(f"There was an error with creating user: {err}", category='danger')
    return render_template('mynewhome.html', form=a_form)

if __name__ == "__main__":
    app.run(debug=True)

Note that you can access data from the a_form instance directly rather than on the request instance.

  • Related