当前位置:   article > 正文

Python Flask 表单的创建与验证。_flask创建表单

flask创建表单
  1. from flask import Flask, render_template, request
  2. from flask_wtf import FlaskForm
  3. from wtforms.fields.simple import StringField, PasswordField, SubmitField
  4. from wtforms.validators import DataRequired, EqualTo
  5. app = Flask(__name__)
  6. app.config['SECRET_KEY'] = ';kjlasdklja'
  7. # 定义表单模型类
  8. class Register(FlaskForm):
  9. username = StringField(label='用户名', validators=[DataRequired("用户名不能为空")])
  10. password = PasswordField(label='密码', validators=[DataRequired("密码不能为空")])
  11. password2 = PasswordField(label='再一次输入密码', validators=[DataRequired("密码不能为空"), EqualTo("password")])
  12. sumbit = SubmitField(label='提交')
  13. @app.route("/register", methods=['POST', 'GET'])
  14. def register():
  15. # 创建表单对象
  16. form = Register()
  17. if request.method == "GET":
  18. return render_template("register.html", form=form)
  19. if request.method == "POST":
  20. # 从前端获取数据
  21. if form.validate_on_submit():
  22. username = form.username
  23. password = form.password
  24. password2 = form.password2
  25. print(username)
  26. print(password)
  27. print(password2)
  28. return render_template("register.html", form=form)
  29. else:
  30. return render_template('404.html')
  31. if __name__ == '__main__':
  32. app.run()
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. Hello world
  9. <br>
  10. {{ data.name }}
  11. <br>
  12. {{ data.age }}
  13. <br>
  14. {{ data.list[1] }}
  15. <br>
  16. {{ "hello_world" | upper | replace('HELLO','HI') }}
  17. <br>
  18. {{ data.list | li2 }}
  19. </body>
  20. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/499312
推荐阅读
相关标签
  

闽ICP备14008679号