#coding=utf-8 from flask import Flask from flask import request from flask import redirect from flask import render_template from wtforms import Form, TextField, PasswordField, validators app = Flask(__name__) class LoginForm(Form): username = TextField("username", [validators.Required()]) password = PasswordField("password", [validators.Required()]) @app.route("/user", methods=["GET", "POST"]) def login_checked(): myForm = LoginForm(request.form) if request.method == "POST": if not myForm.username.data: message = "please input your username" return render_template("index.html", message=message, form=myForm) if not myForm.password.data: message = "please input your password" return render_template("index.html", message=message, form=myForm) if myForm.username.data == "lala" and myForm.password.data == "123456" and myForm.validate(): return redirect("http://www.taobao.com") else: message = "your username or password is wrong" return render_template("index.html", message=message, form=myForm) return render_template("index.html", form=myForm) if __name__ == "__main__": app.run(port=8080)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>user management</title> </head> <body> <div align="center"> <h1>user management</h1> {% if message %} {{ message }} {% endif %} <form method="post" name="form"> username : {{form.username}} <br/> password : {{form.password}} <br/> <input type="submit" name="submit" value="submit"> <input type="reset" name="reset" value="reset"> </form> </div> </body> </html>