Home > Back-end >  FlaskForm validate_on_submit always returns false
FlaskForm validate_on_submit always returns false

Time:05-26

I have been battling with the following code for a while now. The validate_on_submit method always returns false.

I have read a lot of StackOverflow answers to this issue and tried everything I can find, but it still returns False.

The form submits as the print statements for email and password print successfully, but returns false each time.

Can anyone help me to understand what I might be doing wrong here?

main.py

from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import SubmitField, PasswordField, EmailField
from wtforms.validators import DataRequired, Email, Length


class LoginForm(FlaskForm):
    email = EmailField(label="Email", validators=[DataRequired(), Email()])
    password = PasswordField(label="Password", validators=[DataRequired(), Length(min=8)])
    submit = SubmitField(label="Submit")


app = Flask(__name__)
app.secret_key = "*******************"

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/login", methods=["GET", "POST"])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        if form.email.data == "[email protected]" and form.password.data == "12345678":
            return render_template("success.html")
        else:
            render_template("denied.html")
    print("False")
    return render_template("login.html", form=form)


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

And here is the HTML for the form...

login.html

<!DOCTYPE HTML>

<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <div >
        <h1>Login</h1>
            <form method="POST" action="{{ url_for('login') }}" novalidate>
                <p>
                    {{ form.csrf_token }}
                    {{ form.email.label }} <br> {{ form.email(size=30) }}
                    {% for err in form.email.errors %}
                    <span style="color:red">{{ err }}</span>
                    {% endfor %}
                </p>
                <p>
                    {{ form.password.label }} <br> {{ form.password(size=30) }}
                    {% for err in form.password.errors: %}
                    <span style="color:red">{{ err }}</span>
                    {% endfor %}
                </p>
                <p>
                    {{ form.submit }}
                </p>
            </form>
        </div>
    </body>
</html>

Any ideas what I am missing, or doing wrong here?

CodePudding user response:

You missed a return in your main.py

from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import SubmitField, PasswordField, EmailField
from wtforms.validators import DataRequired, Email, Length


class LoginForm(FlaskForm):
    email = EmailField(label="Email", validators=[DataRequired(), Email()])
    password = PasswordField(label="Password", validators=[DataRequired(), Length(min=8)])
    submit = SubmitField(label="Submit")


app = Flask(__name__)
app.secret_key = "*******************"

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/login", methods=["GET", "POST"])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        if form.email.data == "[email protected]" and form.password.data == "12345678":
            return render_template("success.html")
        else:
            return render_template("denied.html") #Was missing
    print("False")
    return render_template("login.html", form=form)


if __name__ == '__main__':
    app.run(debug=True)
  • Related