快速入门Python-Flask·下

2022/3/1 20:51:29

本文主要是介绍快速入门Python-Flask·下,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

快速入门Python-Flask·下

    • 1.使用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()

待更新



这篇关于快速入门Python-Flask·下的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程