快速入门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·下的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门