本文主要是介绍Flask---wtforms使用、信号,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
-
-
- 简单示例
- 基本使用
- 渲染
- 校验
- 自定义Form组件
- 信号
-
简单示例
from flask import Blueprint,render_template,request,session,redirect
from ..utils.sql import SQLHelper
account = Blueprint('account',__name__)from wtforms import Form
from wtforms.fields import simple,core,html5
from wtforms import validators
from wtforms import widgetsclass Myvalidators(object):'''自定义验证规则'''def __init__(self,message):self.message = messagedef __call__(self, form, field):print(field.data,"用户输入的信息")if field.data == "safly":return Noneraise validators.ValidationError(self.message)class LoginForm(Form):user = simple.StringField(validators=[Myvalidators(message="用户名必须是safly"), # 也可以自定义正则validators.DataRequired(message='用户名不能为空.'),# validators.Length(min=6, max=18, message='用户名长度必须大于%(min)d且小于%(max)d')],widget=widgets.TextInput(),render_kw={'class': 'form-control'})pwd = simple.PasswordField(validators=[validators.DataRequired(message='密码不能为空.'),# validators.Length(min=8, message='用户名长度必须大于%(min)d'),# validators.Regexp(regex="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}",# message='密码至少8个字符,至少1个大写字母,1个小写字母,1个数字和1个特殊字符')],widget=widgets.PasswordInput(),render_kw={'class': 'form-control'})@account.route('/login',methods=['GET',"POST"])
def login():if request.method == 'GET':form = LoginForm()return render_template('login.html',form=form)# ImmutableMultiDict([('user', 'safly'), ('pwd', '123')])form = LoginForm(formdata=request.form)print("request.form", request.form, "------", form.data)if not form.validate():return render_template('login.html', form=form)obj = SQLHelper.fetch_one("select id,user from user where user =%(user)s and pwd=%(pwd)s", form.data)if obj:session.permanent = Truesession['user_info'] = {'id':obj['id'], 'user':obj['user']}return redirect('/index')else:return render_template('login.html',msg='用户名或密码错误',form=form)
基本使用
通过pip install wtforms
进行安装
class RegisterForm(Form):name = simple.StringField(label='用户名',validators=[validators.DataRequired()],widget=widgets.TextInput(),render_kw={'class': 'form-control'},default='alex')pwd = simple.PasswordField(label='密码',validators=[validators.DataRequired(message='密码不能为空.')],widget=widgets.PasswordInput(),render_kw={'class': 'form-control'})pwd_confirm = simple.PasswordField(label='重复密码',validators=[validators.DataRequired(message='重复密码不能为空.'),validators.EqualTo('pwd', message="两次密码输入不一致")],widget=widgets.PasswordInput(),render_kw={'class': 'form-control'})email = html5.EmailField(label='邮箱',validators=[validators.DataRequired(message='邮箱不能为空.'),validators.Email(message='邮箱格式错误')],widget=widgets.TextInput(input_type='email'),render_kw={'class': 'form-control'})gender = core.RadioField(label='性别',choices=((1, '男'),(2, '女'),),coerce=int)city = core.SelectField(label='城市',choices=SQLHelper.fetch_all('select id,user from user ',{},None),# choices=(# (1, '篮球'),# (2, '足球'),# ),coerce=int)hobby = core.SelectMultipleField(label='爱好',choices=((1, '篮球'),(2, '足球'),),coerce=int)favor = core.SelectMultipleField(label='喜好',choices=((1, '篮球'),(2, '足球'),),widget=widgets.ListWidget(prefix_label=False),option_widget=widgets.CheckboxInput(),coerce=int,default=[1, 2])def __init__(self, *args, **kwargs):super(RegisterForm, self).__init__(*args, **kwargs)self.city.choices = SQLHelper.fetch_all('select id,user from user ',{},None)def validate_name(self, field):"""自定义pwd_confirm字段规则,例:与pwd字段是否一致:param field::return:"""# 最开始初始化时,self.data中已经有所有的值print(field.data) # 当前name传过来的值# print(self.data) # 当前传过来的所有的值:name,gender.....obj = SQLHelper.fetch_one('select id from user where user =%s',[field.data,])if obj:raise validators.ValidationError("用户名已经存在") # 继续后续验证# raise validators.StopValidation("用户名已经存在") # 不再继续后续验证
渲染
<!DOCTYPE html>
<html lang="zh-cn">
<head><meta 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><form method="post">{% for item in form %}<p>{{item.label}}: {{item}} {{item.errors[0] }}</p>{% endfor %}<input type="submit" value="提交"></form>
</body>
</html>
校验
@account.route('/register',methods=['GET','POST'])
def register():if request.method == 'GET':form = RegisterForm()return render_template('register.html',form=form)form = RegisterForm(formdata=request.form)if form.validate():print("form.data",form.data)else:print("form.errors",form.errors)return render_template('register.html', form=form)
自定义Form组件
#!usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask,render_template,request,Markup
app = Flask(__name__,template_folder="templates")
app.debug = True
# ==============通过这几个类就可以显示了-==============
#插件
class Widget(object):passclass InputText(Widget):def __call__(self, *args, **kwargs):return "<input type='text' name='name'>"class TextArea(Widget):def __call__(self, *args, **kwargs):return Markup("<textarea name='email'></textarea>")#Form
class BaseForm(object):def __init__(self):#获取当前所有的字段_fields = {}for name, field in self.__class__.__dict__.items():if isinstance(field, Field): # 筛选出字段是name和emailDe_fields[name] = fieldself._fields = _fieldsself.data = {}# print(_fields) # {'name': 111, 'email': 222}def validate(self,request_data):#先找到所有的字段,在执行每一个字段的validate方法flag = Truefor name, field in self._fields.items():input_val = request_data.get(name,"") #用户输入的值result= field.validate(input_val) #每一个字段自己校验print("???????????",input_val,result)if not result:flag = Falseelse:self.data[name] = input_valreturn flag
#字段
class Field(object):'''所有类的基类'''def __str__(self): #python中的静态字段通过类能找到,通过对象也能找到return Markup(self.widget()) #self就是StringField,selfclass StringField(Field): #每个字段打印的时候都要去执行__str__,所以选择放在基类里面,自己没有就调用父类的widget = InputText()def validate(self,val):if val:return Trueclass EmaliField(Field):widget = TextArea()reg = ".*@.*"def validate(self,val):import reprint(re.match(self.reg,val),"************")if re.match(self.reg,val):return True# ===============使用===============
class LoginForm(BaseForm):name = StringField()email = EmaliField()@app.route('/index', methods=["GET","POST"])
def index():form = LoginForm()ret = form.validate(request.form)print("验证成功",ret)print("验证成功的值",form.data)# print(form.name)# print(form.email)return render_template("index.html",form=form)if __name__ == '__main__':app.run()
模板
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><form method="post">{{form.email}}{{form.name}}<input type="submit" value="提交">{{msg}}
</form></body>
</html>
信号
from flask import Flask, flash
from flask.signals import _signalsapp = Flask(__name__)xinhao = _signals.signal("xinhao") # 创建信号# 定义函数
def func1(*args, **kwargs):print("func1", args, kwargs)def func2(*args, **kwargs):print("func2", args, kwargs)# 将函数注册到信号中,添加到这个列表
xinhao.connect(func1)
xinhao.connect(func2)@app.route("/zzz")
def zzz():xinhao.send(sender='xxx', a1=123, a2=456) # 触发这个信号,执行注册到列表中的所有函数,这里的参数个上面函数的参数一致return "发送信号成功"if __name__ == '__main__':app.run(debug=True)# 打印结果
# func1 (None,) {'sender': 'xxx', 'a1': 123, 'a2': 456}
# func2 (None,) {'sender': 'xxx', 'a1': 123, 'a2': 456}
这篇关于Flask---wtforms使用、信号的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!