WTF form.validate_on_submit()不起作用 [英] WTF form.validate_on_submit() not working
问题描述
我有以下代码,我正在提交表格.当我单击提交"按钮时,我的表单验证将打印出False
.我已经检查并确保将所有来自不同帖子的内容包括在内,但我无法对其进行验证.我在做错什么吗?
I have the following code and I'm submitting a form. When I hit the submit button, my form validation prints out False
. I've checked and made sure I'm including everything from different posts, but I can't get it to validate. Is there anything I'm doing wrong?
@app.route('/index.html', methods=['GET', 'POST'])
def index():
user = {'nickname': 'Rafa'}
form = FilterForm()
print("about to validate", file=sys.stderr)
if form.validate_on_submit():
print("validated", file=sys.stderr)
filters_array = form.filter.split(',')
streaming(filters_array)
response = {"response", "yes"}
redirect("/authenticate")
return render_template('index.html',
title="Home",
user=user,
form=form)
class FilterForm(Form):
filter = StringField('filter', validators=[DataRequired()])
这是我的Jinja文件
Here is my Jinja file
{% block content %}
<h1> I have successfully navigated to the title pagee </h1>
<h1> Hello, {{user.nickname}}!</h1>
<h1> Get Tweets </h1>
<p> Please enter a comma delimited list of filters</p>
<form action="" method="post" name="login">
{{form.filter(size=80)}}
<input type="submit" value="Get Tweets!">
</form>
{% endblock %}
推荐答案
FilterForm
不应在与def index()
相同的级别上缩进.更重要的是,您的表单中没有csrf_token
.这将阻止其验证.
FilterForm
should not be indented at the same level as def index()
. More importantly, you don't have a csrf_token
in your form. Which will prevent it from validating.
将此添加到您的表单中:
Add this to your form:
{{ form.csrf_token }}
最后,当使用wtforms进行验证时,错误会填充在form对象中.因此,在if validate
之后,尝试打印form.errors
,您将确切找出问题所在.
Lastly, when validating with wtforms, the errors are populated in the form object. So after an if validate
, try printing form.errors
and you'll find out exactly what is wrong.
这篇关于WTF form.validate_on_submit()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!