flask 表单 flask-wtf
flask-wtf
使用flask-wtf表单扩展,可以帮助我们记性CSRF验证,帮助我们快速定义表单模板,而且帮助我们在视图中验证表的数据,不适用时,需要我们自己进行处理
pip3 install Flask-WTF
使用
# main.py
from flask import Flask,url_for,abort,Response,render_template,session,redirect
#下载安装 pip3 install flask_wtf
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField
from wtforms.validators import DataRequired,EqualTo,Length
from ii.forms import RegisterForm
app = Flask(__name__)
app.secret_key="fjioqfndcfjofeqwsc"
#定义表单的模型类,需要写 app包下的 在forms.py文件中
# class RegisterForm(FlaskForm):
# '''
# 自定义的注册表单模型类
# '''
# user_name = StringField(label="用户名",Validators=[DataRequired("用户名不能为空"),Length(10,20,message="不能小于10个字符,不能大于20个字符")],render_kw={"placeholder":"用户名"})
# password = PasswordField(label="密码",ValueError=[DataRequired(message="密码不能为空"),],render_kw={"placeholder":"密码"})
# re_password = PasswordField(label="确认密码",ValueError=[DataRequired(message="密码不能为空"),EqualTo("password",message="两次密码不一致")],render_kw={"placeholder":"确认密码"})
@app.route("/index",methods=["POST","GET"])
def index():
form = RegisterForm() #创建表单对象,不论是get请求还是post请求都会生成表单对象,只有 有没有数据的区别
if form.validate_on_submit(): #判断form中的数据是否合理
#合格之后提取数据
user_name=form.user_name.data
password = form.password.data
re_password = form.re_password.data
print(user_name,password,re_password)
session["user_name"] = user_name
return redirect(url_for("success"))
return render_template("str1.html",form = form)
@app.route("/success",methods=["POST","GET"])
def success():
if session.get("user_name"):
return "ok"
return "未登录"
if __name__ == '__main__':
app.run()
##forms.py
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,SubmitField
from wtforms.validators import DataRequired,EqualTo,Length
class RegisterForm(FlaskForm):
'''
自定义的注册表单模型类
'''
user_name = StringField(label="用户名",validators=[DataRequired("用户名不能为空"),Length(10,20,message="不能小于10个字符,不能大于20个字符")],render_kw={"placeholder":"用户名"})
password = PasswordField(label="密码",validators=[DataRequired(message="密码不能为空")],render_kw={"placeholder":"密码"})
re_password = PasswordField(label="确认密码",validators=[DataRequired(message="密码不能为空"),EqualTo("password",message="两次密码不一致")],render_kw={"placeholder":"确认密码"})
submit = SubmitField(label="提交")
##str1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" novalidate>
{{ form.csrf_token }} {# //csrf要求cookie里有,请求也要有#}
{{ form.user_name.label }}
{{ form.user_name(size=20,id='user_name')}} {# //给这个标签添加属性#}
{# {% for msg in form.user_name.errors %}#}
{# <p>{{ msg }}</p>#}
{#{% endfor %}#}
{{ form.user_name.errors.0 }}
{{ form.password.label }}
{{ form.password }}
{#{% for msg in form.password.errors%}#}
{#<p>{{ msg }}</p>#}
{#{% endfor %}#}
{{ form.password.errors.0 }}
{{ form.re_password.label }}
{{ form.re_password }}
{#{% for msg in form.re_password.errors %}#}
{# <p>{{ msg }}</p>#}
{#{% endfor %}#}
{{ form.re_password.errors.0 }}
{{ form.submit }}
</form>
</body>
</html>