You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Flask-WTF登录表单提交后页面不跳转问题排查

Hey there, let's figure out why your login form isn't redirecting as expected. I’ve spotted a few key issues in your code that are causing this behavior:

1. Missing ReCAPTCHA Field (The Main Culprit)

Your CommentForm class includes a RecaptchaField(), but you never render this field in your login.html template. Flask-WTF validates every field defined in the form class, so since the reCAPTCHA input is missing, form.validate() will always return False. That’s why clicking "Log In" just reloads the login page—your code never hits the redirect logic.

You have two fixes here:

  • Option 1: Remove the reCAPTCHA field if you don’t need it right now:
    class CommentForm(FlaskForm):
        email = StringField(label="Email", validators=[Email()])
        password = PasswordField(label="Password", validators=[DataRequired(), Length(min=8)])
        submit = SubmitField(label="Log In")
        # Delete this line: recaptcha = RecaptchaField()
    
  • Option 2: Add the reCAPTCHA field to your template if you want to keep it (make sure you’ve configured reCAPTCHA keys in your Flask app first):
    Insert this code right before the submit button in login.html:
    {{ form.recaptcha }}
    {% for err in form.recaptcha.errors %}
    <span style="color:red">{{err}}</span>
    {% endfor %}<br>
    

2. Incorrect Form Instantiation

You’re passing request.form when creating your CommentForm instance, but Flask-WTF automatically handles request data for you. Just instantiate the form without arguments:

@app.route("/login", methods=['GET', 'POST'])
def login():
    # Replace this: form = CommentForm(request.form)
    form = CommentForm()
    if request.method == 'POST' and form.validate():
        if form.email.data == "admin@email.com" and form.password.data == "12345678":
            return render_template("success.html")
        else:
            return render_template("denied.html")
    return render_template("login.html", form=form)

3. Unnecessary rows Attribute for Single-Line Inputs

StringField and PasswordField are designed for single-line input, so using rows=1 doesn’t make sense (that’s for multi-line TextAreaField). You can either remove the rows and cols attributes, or use size to set the input width:

<!-- Replace form.email(rows=1, cols=30) with -->
{{ form.email(size=30) }}
<!-- Replace form.password(rows=1, cols=30) with -->
{{ form.password(size=30) }}

Once you fix these issues, your form should validate correctly and redirect to the success.html or denied.html page depending on the credentials entered.

内容的提问来源于stack exchange,提问作者Greta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 18:38:12