flask_wtf 表单
forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import length, email, DataRequired
class LoginForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired(), length(min=6, max=20)])
remember = BooleanField('Remember me')
submit = SubmitField('Log in')
视图函数
@app.route("/test", methods=["GET", "POST"])
def test():
form = LoginForm()
if request.method == "POST":
print(form.data)
return "success"
else:
content = {
"form": form
}
return render_template("test.html", **content)
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/test">
{{ form.csrf_token }}
{{ form.username.label }}<br/>{{ form.username }}<br/>
{{ form.password.label }}<br/>{{ form.password }}<br/>
{{ form.remember }}{{ form.remember.label }}<br/>
{{ form.submit }}<br/>
</form>
</body>
</html>
标签:form,flask,label,wtf,validators,test,表单
From: https://www.cnblogs.com/bitterteaer/p/16755842.html