form class:
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,BooleanField,SubmitField
from wtforms.validators import DataRequired,Length
class LoginForm(FlaskForm):
username = StringField(":",validators=[DataRequired()])
password = PasswordField(":",validators=[DataRequired(),Length(8,128)])
remember = BooleanField("")
submit = SubmitField("")
APP routing:
from flask import Flask,render_template,redirect,url_for,flash
from demos.form.forms import LoginForm
app = Flask(__name__)
app.config["SECRET_KEY"] = "fasdfas2342"
app.secret_key = "secret string"
@app.route("/")
def index():
return "Hello Flask"
@app.route("/login",methods=["GET","POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
username = form.username.data
flash("welcome home , %s" % username)
print(username)
return redirect(url_for("index"))
return render_template("login.html" , form=form)
template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>-Henglu</title>
<link rel="stylesheet" type="text/css" href="{{ url_for("static",filename="css/login.css") }}" >
<script type="text/javascript" src="{{ url_for("static",filename="jQuery/jquery-3.3.1.js") }}"></script>
<script type="text/javascript" src="{{ url_for("static",filename="js/login.js") }}"></script>
</head>
<body>
<div id="body">
<div id="logo">
<img src="{{ url_for("static",filename="img/logo.jpg") }}">
</div>
<div id="form">
<form method="post">
{{ form.csrf_token }}
{{ form.username.label }} {{ form.username }}<br>
{{ form.password.label }} {{ form.password }}<br><br>
{{ form.remember }} {{form.remember.label }}<br><br>
{{ form.submit }}<br>
</form>
</div>
</div>
</body>
</html>
Post cannot redirect and does not report an error after submitting data?