wtf表单接收post请求后 无法跳转到其他页面

问题:当我在表单里输入数据,点击提交后,应该是跳转到首页,但是实际上并没有任何跳转

1.这是表单类
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,BooleanField,SubmitField
from wtforms.validators import DataRequired,Length

class BaseForm(FlaskForm):
    username = StringField("用户名",validators=[DataRequired(message="数据无效"),Length(8,20,message="用户名必须在8到20位之间")])
    password = PasswordField("密码",validators=[DataRequired(message="数据无效"),Length(8,20,message="密码必须在8到20位之间")])
    login = SubmitField("登录")``


2.这是视图函数
from flask import Flask,render_template,url_for,flash,redirect,request
from forms import BaseForm


app = Flask(__name__)
app.secret_key = "secret string"
app.config["WTF_I18N_ENABLED"] = False



# 首页
@app.route("/")
def index():
    return render_template("index.html")


# 测试页面
@app.route("/login",methods=["GET","POST"])
def login():
    login_form = BaseForm()
    if login_form.validate_on_submit():
        username = login_form.username.data
        password = login_form.password.data
        print(username)
        print(password)
        flash("欢迎回来,请记住你的用户名是{},你的密码是{}".format(username, password))
        return redirect(url_for("index"))
    else:
        return render_template("login.html", login_form=login_form)



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


3.这是宏
{% macro baseForm(func) %}
    {{ func.label }}
    {{ func(**kwargs) }}
    {% if func.errors %}
        {% for error in func.errors %}
            <smal class="error" style="color:red;">{{ error }}</smal>
        {% endfor %}
    {% endif %}
    <br>
{% endmacro %}


4.这是表单渲染的代码
{% extends "base.html" %}
{% from "macros.html" import baseForm %}


{% block title %}
测试
{% endblock %}


{% block head %}

{% endblock %}



{% block body %}

    <form action="{{ url_for("login") }}" method="post">

    {{ baseForm(login_form.username) }}
    {{ baseForm(login_form.password) }}
    {{ baseForm(login_form.login) }}


    </form>


{% endblock %}


【补充一下】
视图函数写成这样子是可以运行的:

@app.route("/login",methods=[“GET”,“POST”])
def login():
login_form = BaseForm()
if request.method == “GET”:
return render_template(“login.html”,login_form=login_form)
else:
username = login_form.username.data
password = login_form.password.data
print(username)
print(password)
return redirect(url_for(“index”))
# if login_form.validate_on_submit():
# username = login_form.username.data
# password = login_form.password.data
# print(username)
# print(password)
# flash(“欢迎回来,请记住你的用户名是{},你的密码是{}”.format(username, password))
# return redirect(url_for(“index”))
# else:
# return render_template(“login.html”, login_form=login_form)

【如果用到validate验证的话就不行了】 @app.route("/login",methods=[“GET”,“POST”])
def login():
login_form = BaseForm()
if request.method ==“POST” and login_form.validate():
username = login_form.username.data
password = login_form.password.data
print(username)
print(password)
return redirect(url_for(“index”))

return render_template(“login.html”,login_form=login_form)

# if login_form.validate_on_submit():
# username = login_form.username.data
# password = login_form.password.data
# print(username)
# print(password)
# flash(“欢迎回来,请记住你的用户名是{},你的密码是{}”.format(username, password))
# return redirect(url_for(“index”))
# else:
# return render_template(“login.html”, login_form=login_form)

进入到这个分支没有?有没有打印username和password?

没有

pycharm的运行记录显示到表单想/login发起post请求之后就没了image

顶帖,还未解决

是不是因为没有渲染csrf_token导致提交失败?
form.validate()和form.validate_on_submit()是要验证这个的

    <form action="{{ url_for("login") }}" method="post">
    {{ form.csrf_token }}
    {{ baseForm(login_form.username) }}
    {{ baseForm(login_form.password) }}
    {{ baseForm(login_form.login) }}
    </form>

是的 把这个忘了