快速入门Python-Flask·下

快速入门Python-Flask·下

1.使用Flask表单

使用Flask提供的后端表单

main.py

from flask import Flask,render_template,request
# 导入类型的包,字符串,密码,提交
from wtforms import StringField,PasswordField,SubmitField
from flask_wtf import FlaskForm
# 验证的包,不能为空,数据是否相同
from wtforms.validators import DataRequired,EqualTo


app=Flask(__name__)

# 防止攻击手段,后面字符串为随意字符串
app.config['SECRET_KEY'] = 'AQWESDFK'

# 定义表单模型类
class Register(FlaskForm):
    username = StringField(label="用户名",validators=[DataRequired('用户名不能为空')])
    password = StringField(label="密码", validators=[DataRequired('密码不能为空')])
    agpassword = StringField(label="再次输入密码", validators=[DataRequired('密码不能为空'),EqualTo('password')])
    submit = SubmitField(label="提交")

@app.route('/',methods=['GET','POST'])
def register():
    # 创建表单对象
    form = Register()

    if request.method == 'GET':
        return render_template('index.html',form=form)
    if request.method == 'POST':
        username = form.username.data
        password = form.password.data
        agpassword = form.agpassword.data
        print(username)
        print(password)
        print(agpassword)
        return render_template('index.html', form=form)

if __name__ == '__main__':
    app.run()

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单</title>
</head>
<body>
<img src="static/header.jpg">
<form action="" method="post">
    {{ form.username.label }}
    {{ form.username }}
    {{ form.password.label }}
    {{ form.password }}
    {{ form.agpassword.label }}
    {{ form.agpassword }}
    {{ form.submit }}
</form>

</body>
</html>

但是发现,前端输入了两个不相同的密码,依然传递给了后端

解决方法:加入验证器

index.html,注意添加:{{form.csrf_token}}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单</title>
</head>
<body>
<img src="static/header.jpg">
<form action="" method="post">
    {{form.csrf_token}}
    {{ form.username.label }}
    {{ form.username }}
    {{ form.password.label }}
    {{ form.password }}
    {{ form.agpassword.label }}
    {{ form.agpassword }}
    {{ form.submit }}
</form>

</body>
</html>

main.py

from flask import Flask,render_template,request
# 导入类型的包,字符串,密码,提交
from wtforms import StringField,PasswordField,SubmitField
from flask_wtf import FlaskForm
# 验证的包,不能为空,数据是否相同
from wtforms.validators import DataRequired,EqualTo


app=Flask(__name__)

# 防止攻击手段,后面字符串为随意字符串
app.config['SECRET_KEY'] = 'AQWESDFK'

# 定义表单模型类
class Register(FlaskForm):
    username = StringField(label="用户名",validators=[DataRequired('用户名不能为空')])
    password = StringField(label="密码", validators=[DataRequired('密码不能为空')])
    agpassword = StringField(label="再次输入密码", validators=[DataRequired('密码不能为空'),EqualTo('password')])
    submit = SubmitField(label="提交")

@app.route('/',methods=['GET','POST'])
def register():
    # 创建表单对象
    form = Register()

    if request.method == 'GET':
        return render_template('index.html',form=form)
    if request.method == 'POST':
        # 加入验证器,确保数据是正确的
        if form.validate_on_submit():
            username = form.username.data
            password = form.password.data
            agpassword = form.agpassword.data
            print(username)
            print(password)
            print(agpassword)
        else:
            print('验证失败!')
        return render_template('index.html', form=form)

if __name__ == '__main__':
    app.run()

待更新

上一篇:Android之OkHttp网络架构源码深入分析(揭开神秘的面纱),android项目开发实战入门pdf


下一篇:phpstorm的html网页乱码