Raf*_*afa 1 flask wtforms flask-wtforms
我有以下代码,我正在提交表格.当我点击提交按钮时,我的表单验证打印出来False.我已经检查并确保我包含了不同帖子中的所有内容,但我无法让它验证.有什么我做错了吗?
@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()])
Run Code Online (Sandbox Code Playgroud)
这是我的Jinja文件
{% 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 %}
Run Code Online (Sandbox Code Playgroud)
FilterForm不应该缩进到与...相同的水平def index().更重要的是csrf_token,您的表单中没有.这将阻止它验证.
将其添加到您的表单:
{{ form.csrf_token }}
Run Code Online (Sandbox Code Playgroud)
最后,在使用wtforms进行验证时,会在表单对象中填充错误.所以在if validate尝试打印之后form.errors,你会发现到底出了什么问题.
| 归档时间: |
|
| 查看次数: |
5863 次 |
| 最近记录: |