本文主要是介绍Flask入门教程(3)-表单验证和WTF扩展,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
03-01-普通的表单验证
03-02-flash消息闪现
html代码:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<form method="post"><lable>用户名:</lable><input type="text" name="username"><br><lable>密码:</lable><input type="password" name="password"><br><lable>确认密码:</lable><input type="password" name="password2"><br><input type="submit" value="提交">{# 使用遍历获取闪现的消息 #}{% for message in get_flashed_messages() %}{{ message }}{% endfor %}</form>
</body>
</html>
python代码:
from flask import Flask, render_template, request, flashapp = Flask(__name__)
app.secret_key = 'itheima'
'''
目的:实现一个简单的登录的逻辑处理
1.路由需要有get和post两种请求方式 --> 需要判断请求方式
2.获取请求的参数
3.判断参数是否填写 & 密码是否相同
4.如果判断都没有问题,就返回一个success
'''
'''
给模板传递消息
flash --> 需要对内容加密,因此需要时何止secret_key,做加密消息的混淆
模板中需要遍历消息
'''@app.route('/', methods=['GET', 'POST'])
def index():# request: 请求对象 --> 获取请求方式、数据# 1.判断请求方式if request.method == 'POST':# 2.获取请求的参数username = request.form.get('username')password = request.form.get('password')password2 = request.form.get('password2')print(username)# 3.判断参数是否填写 & 密码是否相同if not all([username, password, password2]):# print('参数不完整')flash('参数不完整')elif password != password2:# print('密码不一致')flash('密码不一致')else:return 'success'return render_template('index.html')if __name__ == '__main__':app.run()
03-03-WTF简介
03-04-WTF表单的显示
html代码
<hr><form method="post">{{ form.username.label }}{{ form.username }}<br>{{ form.password.label }}{{ form.password }}<br>{{ form.password2.label }}{{ form.password2 }}<br>{{ form.submit }}<br>
</form>
python代码
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
'''
使用WTF实现表单
自定义表单类
'''
class LoginForm(FlaskForm):username = StringField('用户名:')password = PasswordField('密码:')password2 = PasswordField('确认密码:')submit = SubmitField('提交:')@app.route('/form', methods=['GET', 'POST'])
def login():login_form = LoginForm()return render_template('index.html', form=login_form)
03-05-WTF的逻辑验证
html代码
<hr><form method="post">{{ form.csrf_token() }}{{ form.username.label }}{{ form.username }}<br>{{ form.password.label }}{{ form.password }}<br>{{ form.password2.label }}{{ form.password2 }}<br>{{ form.submit }}<br>
</form>
python代码
from wtforms.validators import DataRequired, EqualToclass LoginForm(FlaskForm):username = StringField('用户名:', validators=[DataRequired()])password = PasswordField('密码:', validators=[DataRequired()])password2 = PasswordField('确认密码:', validators=[DataRequired(), EqualTo('password', '密码填入不一致')])submit = SubmitField('提交:')@app.route('/form', methods=['GET', 'POST'])
def login():login_form = LoginForm()# 1.判断请求方式if request.method == 'POST':# 2.获取请求的参数username = request.form.get('username')password = request.form.get('password')password2 = request.form.get('password2')print(password)# 3.验证参数.WTF可以一句话就实现所有的校验# 我们没有CSRF_tokenif login_form.validate_on_submit():print(username, password)return 'success'else:flash('参数有误')return render_template('index.html', form=login_form)
这篇关于Flask入门教程(3)-表单验证和WTF扩展的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!