WTF form.validate

编程入门 行业动态 更新时间:2024-10-17 12:22:54
本文介绍了WTF form.validate_on_submit()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码,我正在提交表格.当我单击提交"按钮时,我的表单验证将打印出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

本文发布于:2023-11-26 20:43:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635126.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WTF   form   validate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!