【发布时间】:2016-03-15 13:25:04
【问题描述】:
我正在学习如何在一个新的应用程序概念证明中一起使用 WTForms 和 Flask。
我有一个表格。目标是要求姓氏至少包含 3 个字符。
class PersonByNameForm(Form):
first_name = StringField('First Name', filters=none_filter)
last_name = StringField(validators=[InputRequired('Enter a Last Name'), Length(min=3)])
submit = SubmitField('SUBMIT')
我这样渲染表单
@app.route('/', methods=['GET'])
def index():
if request.method == 'GET':
form = PersonByNameForm()
return render_template('front_page.html', form=form)
html
<form action="person_profiles" method="post">
{{form.hidden_tag()}}
{{form.first_name.label}}
{{form.first_name}}
{{form.last_name.label}}
{{form.last_name}}
{{form.submit}}
</form>
表单本身将数据发布到
@app.route('/person_profiles', methods=['GET', 'POST'])
def person_profiles():
if request.method == 'GET':
# This is just place holder but this view will have copy of the form
form = PersonByNameForm()
form2 = FindPersonForm()
return render_template('person_profile.html', context=[], form=form, form2=form2)
else:
form = PersonProfileForm(request.form)
if form.validate_on_submit():
query = Session.query(schema.Person)
first_name = form.first_name.data
last_name = form.last_name.data
print(first_name, last_name)
if first_name:
query = query.filter(schema.Person.first_name.contains(first_name))
if last_name:
query = query.filter(schema.Person.last_name.contains(last_name))
return render_template('person_profile.html', context=query.all())
else:
print(form.errors)
error = form.errors
flash_errors(form, 'test')
return render_template('person_profile.html', error=error, form=form)
表单将正确validate_on_submit(),并进入else块打印{'last_name': ['Enter a Last Name']}的form.error
我遇到的问题是,我不想实际渲染模板(它就在那里,因为我现在必须返回响应)。并且屏幕上没有闪烁任何错误。
如何限制导航直到 x 个字符出现在框中?如果它没有验证并闪烁消息?
感谢阅读
【问题讨论】:
-
投反对票的人能否评论一下为什么?
标签: python flask wtforms flask-wtforms