WTF表单
wtf.py
pip install flask-wtf # 安装
from flask_wtf import FlaskForm from wtform import StringField, PasswordField, SubmmitField app.config["SECRET_KEY"] = "12345678" class register(flaskform): username = StringField("用户名:", render_kw={"placeholder":"占位符"}) password = PasswordField("密码:") password2 = PassswordField("确认密码:") submit = SubmitField("提交")
from flask import Flask, render_template from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, SubmitField app = Flask(__name__) class register(FlaskForm): username = StringField("用户名:", render_kw={‘placeholder‘: "我是占位符"}) password = PasswordField("密码:") password2 = PasswordField("确认密码") submit = SubmitField("注册") @app.route("/",methods=["POST", "GET"]) def index(): registerform = register() return render_template("demo4_template.html", form=registerform) if __name__ == ‘__main__‘: app.run()
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {# <form method="post">#} {# <label>用户名:</label><input type="text" name="username" placeholder="请输入用户名"><br/>#} {# <label>密码:</label><input type="password" name="password" placeholder="请输入密码"><br/>#} {# <label>确认密码:</label><input type="password" name="password2" placeholder="请输入确认密码"><br/>#} {# <input type="submit" value="注册">#} {##} {#</form>#} <br/> <br/> <br/> <form method = "post"> {{ form.username.label }}{{ form.username }}<br/> {{ form.password.label }}{{ form.password }}<br/> {{ form.password2.label }}{{ form.password2 }}<br/> {{ form.submit}} </form> </body> </html>
CSRF
Flask-SQLAlchemy
flask-sqlalchemy 扩展
pip install flask-sqlalchemy flask-mysqldb #安装
【Flask】 python学习第一章 - 6.0 WTF表单 数据库
原文:https://www.cnblogs.com/oscarli/p/12070760.html