htm page:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form method="post"> {{form.csrf_token()}} {{form.username.label}}{{form.username}} {{form.password.label}}{{form.password}} {{form.password2.label}}{{form.password2}} {{form.submit}} </form> </body> </html>
Demo02:
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 App = the Flask ( __name__ ) # Flash needs to be encrypted content app.secret_key = " the Test " class LoginForm(FlaskForm): username = StringField(u"用户名:", validators=[DataRequired()]) password = PasswordField(u"密码:", validators=[DataRequired()]) password2 = PasswordField (U " Confirm Password: " , validators = [DataRequired (), EqualTo ( " password " , " password inconsistency " )]) submit = SubmitField("提交") @app.route("/", methods=["GET", "POST"]) def login(): login_form = the LoginForm () # 1. Analyzing Request Mode IF request.method == " the POST " : # 2. Get request parameter username = request.form.get ( " username " ) password = request.form.get("password") password2 = request.form.get ( " password2 " ) # 3. Verify, to achieve all of the check by the WTF can sentence IF login_form.validate_on_submit (): Print (username, password, password2) return " login success " the else : Flash ( " Parameter error " ) return the render_template ( " the login.html " , form = login_form) # 4. launcher IF the __name__ == ' __main__ ' : app.run()
Test results: