一、后端app.py
#coding=utf-8
from flask import Flask, render_template, request,flash
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,SubmitField
from wtforms.validators import DataRequired,EqualTo,Length
app = Flask(__name__)
app.secret_key="adfa"
class LoginForm(FlaskForm):
username=StringField(label=u'用户名:',validators=[DataRequired(u'用户名不能为空'),Length(3,20,u'用户名长度必须3~64位字符')])
password=PasswordField(label=u'密码:',validators=[DataRequired(),Length(6,12,u'密码必须位6~12字符')])
password2 = PasswordField(label=u'确认密码:',validators=[DataRequired(u'确认密码不能为空'),EqualTo('password',u'两次密码不一致')])
submit = SubmitField(label=u'登录')
@app.route("/a12",methods=["GET","POST"])
def form():
form=LoginForm()
if request.method=="POST":
if form.validate_on_submit():
username = request.form.get("username")
password = request.form.get("password")
password2 = request.form.get("password2")
print(username,password,password2)
flash("success!")
return render_template("a12.html",login=form)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=True)
二、前端('/templates/a12.html')
<html lang="cn-zh">
<body>
<form action="a12" method="post">
{{ login.csrf_token }}
{{login.username.label}}{{login.username}}
{% for msg in login.username.errors %}
<font color="red" >{{msg }}</font>
{% endfor %}<br>
{{login.password.label}}{{login.password}}
{% for msg in login.password.errors %}
<font color="red" >{{msg }}</font>
{% endfor %}<br>
{{login.password2.label}}{{login.password2}}
{% for msg in login.password2.errors %}
<font color="red" >{{msg }}</font>
{% endfor %}<br>
{{login.submit}}<br>
{% for message in get_flashed_messages() %}
<font color="blue" >{{message }}</font>
{% endfor %}
</form>
</body>
</html>