FlaskForm validate_on_submit 始终 returns 错误

FlaskForm validate_on_submit always returns false

我已经和下面的代码斗争了一段时间了。 validate_on_submit 方法总是 returns false。

我已经阅读了很多关于这个问题的 Whosebug 答案并尝试了我能找到的所有方法,但它仍然 returns 错误。

表单提交为电子邮件和密码的打印语句打印成功,但每次都returns错误。

任何人都可以帮助我了解我在这里可能做错了什么吗?

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 == "admin@email.com" 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)

这是表格的 HTML...

login.html

<!DOCTYPE HTML>

<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <div class="container">
        <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>

知道我遗漏了什么或做错了什么吗?

您在 main.py

中错过了一个 return
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 == "admin@email.com" 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)