Flask wtforms实现简单的登录注册
目录结构
视图
# -*- coding:utf-8 -*- # Author : Niuli # Data : 2019-02-27 19:26 from flask import render_template from flask import request from flask import session from flask import views from flask import Blueprint from wtforms import Form,validators,widgets from wtforms.fields import simple,core user_bp = Blueprint('user_bp',__name__) # class User(views.MethodView): def get(self): return 'hello' user_bp.add_url_rule('/', view_func=User.as_view(name='index')) class LoginForm(Form): username = simple.StringField( label='用户名:', validators=[ validators.DataRequired(message='用户名不能为空'), validators.Length(min=4,max=8,message='用户名不符合规定(大于4位且小于8位)'), ] ) password = simple.PasswordField( label='密码:', validators=[ validators.Length(min=3,max=8,message='密码不能小于3位或大于8位'), validators.DataRequired(message='密码不能为空!') ] ) class Login(views.MethodView): def get(self): login_form = LoginForm() return render_template('login.html', loginform=login_form) def post(self): # 校验 user_info = LoginForm(request.form) # 如果校验成功 if user_info.validate(): session['user'] = user_info.data.get('username') return str(session['user']) return render_template('login.html',loginform=user_info) user_bp.add_url_rule('/login', view_func=Login.as_view(name='login')) class RegForm(Form): username = simple.StringField( label='用户名: ', validators=[ validators.DataRequired(message='用户名不能为空'), ], render_kw={'class': 'my_class'} # 更改标签class ) pwd = simple.PasswordField( label='密码: ', validators=[ validators.DataRequired(message='密码不能为空'), validators.Length(min=4,max=8) ] ) repwd = simple.PasswordField( label='确认密码: ', validators=[ validators.EqualTo('pwd',message='两次密码不一致') ] ) gender = core.SelectField( label='性别: ', choices=( (1,'男'), (2,'女') ), default=1, coerce=int ) email = simple.StringField( label='邮箱: ', validators=[ validators.Email(message='邮箱格式有误!') ] ) hobby = core.SelectMultipleField( label='爱好: ', choices=( (1,'吃'), (2,'喝'), (3,'玩'), (4,'乐') ), coerce=int, default=(1,2) ) class Regist(views.MethodView): def get(self): reg_info = RegForm() print('*'*88) return render_template('reg.html',re=reg_info) def post(self): reg_info = RegForm(request.form) if reg_info.validate(): return '注册成功!' return render_template('reg.html',re=reg_info) user_bp.add_url_rule('/reg',view_func=Regist.as_view(name='reg'))
模板
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="content-Type" charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> </head> <body> <div id="app"> <form action="" method="post" novalidate> <p>{{ loginform.username.label }}{{ loginform.username }}{{ loginform.username.errors[0] }}</p> <p>{{ loginform.password.label }}{{ loginform.password }}{{ loginform.password.errors.0 }}</p> <button>18🈲</button> </form> </div> <script> //<!-- 组件创建 --> //<!--组件注册--> </script> </body> </html>
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="content-Type" charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> </head> <body> <div id="app"> <form action="" method="post" novalidate> {% for foo in re %} <p>{{ foo.label }}{{ foo }}{{ foo.errors.0 }}</p> {% endfor %} <button> 💙💔🧡 </button> </form> </div> <script> //<!-- 组件创建 --> //<!--组件注册--> </script> </body> </html>
App中的__init__文件
from flask import Flask from flask_session import Session from redis import Redis from session_wtforms.views.users import user_bp def create_app(): app = Flask(__name__,template_folder='templates') app.config['DEBUG'] = True app.config['SESSION_TYPE'] = 'redis' app.config['SESSION_REDIS'] = Redis() app.config['SESSION_COOKIE_NAME'] = 'SESSION名字' Session(app) app.register_blueprint(user_bp) return app
manage
from session_wtforms import create_app app = create_app() if __name__ == '__main__': app.run(debug=True,port=9808)
wtforms组件 (需pip3安装)
导入
from wtforms import Form,validators,widgets from wtforms.fields import simple,core
应用
class RegForm(Form): username = simple.StringField( label='用户名: ', validators=[ validators.DataRequired(message='用户名不能为空'), ], render_kw={'class': 'my_class'} # 更改标签class ) pwd = simple.PasswordField( label='密码: ', validators=[ validators.DataRequired(message='密码不能为空'), validators.Length(min=4,max=8) ] ) repwd = simple.PasswordField( label='确认密码: ', validators=[ validators.EqualTo('pwd',message='两次密码不一致') ] ) gender = core.SelectField( label='性别: ', choices=( (1,'男'), (2,'女') ), default=1, coerce=int ) email = simple.StringField( label='邮箱: ', validators=[ validators.Email(message='邮箱格式有误!') ] ) hobby = core.SelectMultipleField( label='爱好: ', choices=( (1,'吃'), (2,'喝'), (3,'玩'), (4,'乐') ), coerce=int, default=(1,2) )
校验
def post(self): reg_info = RegForm(request.form) #获取校验数据 if reg_info.validate(): #如果校验成功 return '注册成功!' return render_template('reg.html',re=reg_info)