flask 表单 flask-wtf

2019-07-07 15:14:55 浏览数 (1)

from flask import Flask from flask import render_template from flask_bootstrap import Bootstrap from flask_wtf import Form from wtforms import StringField,SubmitField from wtforms.validators import Required

class NameForm(Form): name=StringField("What is your name?",validators=[Required()]) submit=SubmitField("Submit")

app = Flask(name) bootstrap=Bootstrap(app) app.config['SECRET_KEY'] = 'hard to guess string'

@app.route('/',methods=['GET','POST']) def name(): name=None form=NameForm() if form.validate_on_submit(): name=form.name.data form.name.data='' return render_template('name.html',form=form,name=name)

if name == 'main': app.run()

###########name.html {% extends "bootstrap/base.html" %} {% import "bootstrap/wtf.html" as wtf %} {% block title %}flask-wtf{% endblock %} {% block body %} <div class="page-header"> <h1>Hello, {% if name %}{{ name }}{% else %}Stranger{% endif %}!</h1> </div> {{ wtf.quick_form(form) }} {% endblock %}

//或者 <form method="POST"> {{ form.hidden_tag() }} {{ form.name.label }} {{ form.name(id='my-text-field') }} {{ form.submit() }} </form>


2、

支持

StringField TextAreaField PasswordField HiddenField DateField DateTimeField IntegerField DecimalField FloatField BooleanField 复选框 RadioField SelectField SelectMultipleField FileField 文件上传 SubmitField FormField FieldList 3、

验证

Email EqualTo IPAddress Length NumberRange Optional Required 确保有数据 Regexp 正则 URL AnyOf 在值内 NoneOf 不在

0 人点赞