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