其他分享
首页 > 其他分享> > 【Flask】wtforms表单验证

【Flask】wtforms表单验证

作者:互联网

forms.py

import wtforms
from wtforms.validators import length,email

# 登录的验证器
class LoginForm(wtforms.Form):
    email = wtforms.StringField(validators=[length(min=5,max=20),email()])
    password = wtforms.StringField(validators=[length(min=6,max=20)])

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<form action="/login" method="post">
    <tbody>
        <tr>
            <td>邮箱:</td>
            <td><input type="text" name="email"></td>
        </tr>
        <tr>
            <td>密码:</td>
            <td><input type="text" name="password"></td>
        </tr>
        <tr>
            <td><button>登录</button></td>
            <td></td>
        </tr>
    </tbody>
</form>
</body>
</html>

app.py

from flask import Flask,request,render_template
from forms import LoginForm

app = Flask(__name__)


@app.route('/')
def hello_world():  # put application's code here
    return 'Hello World!'

@app.route('/login',methods=['GET','POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    else:
        form = LoginForm(request.form)
        if form.validate():
            return '登录成功'
        else:
            return "邮箱或密码错误!"

if __name__ == '__main__':
    app.run()

标签:__,return,Flask,app,表单,wtforms,import,login
来源: https://blog.csdn.net/Erin_jwx/article/details/121630987