python 06 - Flask认证模块 - 表单对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 06 - Flask认证模块 - 表单对象相关的知识,希望对你有一定的参考价值。

# coding: utf-8 
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,BooleanField,SubmitField,ValidationError
from wtforms.validators import DataRequired,Length,Email,EqualTo,Regexp
from .models import User

class LoginForm(FlaskForm):
    email = StringField("Email",validators=[DataRequired(),Length(1,64),Email()])
    password = PasswordField("Password",validators=[DataRequired()])
    remember_me = BooleanField("Keep me Logged in")
    submit = SubmitField("Log In")
class RegisterForm(FlaskForm):
    name = StringField("Name",validators=[DataRequired(),Regexp('^[A-Za-z][A-Za-z0-9_.]*$',0,'Usernames must have only letters, numbers, dots or underscores')])
    email = StringField("Email",validators=[DataRequired(),Length(1,64),Email()])
    password1 = PasswordField("Password",validators=[DataRequired()])
    password2 = PasswordField("Password Confirm",validators=[DataRequired(),EqualTo('password1')])    
    submit = SubmitField("Register")

    def validate_email(self,field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError("Email registered")
    
    def validate_name(self,filed):
        if User.query.filter_by(name=filed.data).first():
            raise ValidationError("name registered")

以上是关于python 06 - Flask认证模块 - 表单对象的主要内容,如果未能解决你的问题,请参考以下文章

python 03 - Flask认证模块 - 视图处理

python 07 - Flask认证模块 - 验证码/发送邮件

python 02-Flask认证模块 - 数据库设计

ldap+flask+python2实现统一认证里面的那些编码神坑

Flask - WSGI - 没有名为“flask”的模块

Flask+vue的Token认证