为什么我的烧瓶表格验证返回不是有效选择? [英] Why is my flask form validation returning Not a valid choice?

查看:79
本文介绍了为什么我的烧瓶表格验证返回不是有效选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图弄清楚为什么我的Flask表单无法正确验证我的选择字段选择,即使这些选择来自选择字段选项.

I have been trying to figure out why my Flask form will not properly validate my select field choices even though the choices are coming from the select field options.

我的假设是,从服务器传回的选择选项是unicode,并且正在与字符串选择进行比较,但是,我认为coerce = str可以解决该问题.我打印出表单数据并请求下面的输出数据.为什么不起作用?

My assumption is that the select option when passed back from the server is unicode and is being compared to the choice which is a string, however, I thought coerce=str would fix that. I printed out the form data and request data which is the output below. Why isn't it working?

我的代码附在下面,从输出字典中删除了csrf令牌密钥.看起来很简单,但是我无法弄清楚.

My code is attached below, removed csrf token key from the output dict. It seems like a very simple thing, but I can't figure it out.

forms.py

class PlatformForm(FlaskForm):
    platform_options = [('test', 'Test'), ('test2','Test2')]
    platforms = wtforms.SelectField('Platforms', choices=platform_options, coerce=str, validators=[DataRequired()])

views.py

@app.route('/', methods=['POST', 'GET'])
def index():
    form = forms.PlatformForm()
    if form.is_submitted():
        print form.data
        print request.form
    if form.errors:
        print form.errors
return render_template('home.html', form=form)

index.html

   {% extends "base.html" %}
   {% block content %}
   <h4>Select a Platform</h4>
   <form method="POST">
       {{ form.csrf_token }}
       <select class="custom-select" name="platform">
       {% for value, text in form.platforms.choices %}<br>
           <option value="{{ value }}">{{ text }}</option>
       {% endfor %}
       </select>
<button id="submit_inputs" type="submit" class="btn btn-default">Submit</button>
    </form>
    {% endblock %}

输出

output

   {'platforms': 'None'}
   ImmutableMultiDict([('platform', u'test')])
   {'platforms': [u'Not a valid choice']}

我解决了这个问题.这就是我通过HTML和Jinja创建Select下拉菜单的方式.遍历选择并创建选项标签时,似乎没有实例化表单数据本身中返回到Python的任何内容.将整个for循环更改为

I figured out the problem. It's the way I'm creating the Select drop down through HTML and Jinja. Iterating through the choices and creating option tags doesn't seem to instantiate anything in the form data itself when passed back into Python. Changing that whole for loop to just

{{form.platforms}}

创建了一个实际起作用的选择下拉字段.

created a select drop down field that actually works.

推荐答案

您的姓名不匹配.在表单中,您将选择字段命名为platforms(复数).在HTML中,您使用platform(单数).

You have a name mismatch. In the form, you named your select field platforms (plural). In the HTML, you use platform (singular).

我建议您让WTForms为您生成HTML,而不是手动呈现模板中的字段.对于表单标签,可以使用{{ form.platforms.label }},对于实际字段可以使用{{ form.platforms() }}.您可以将要字段具有的任何属性作为关键字参数传递.

I recommend that instead of manually rendering the fields in your template, you let WTForms generate the HTML for you. For the form label, you can use {{ form.platforms.label }}, and for the actual field {{ form.platforms() }}. You can pass any attributes you want to field to have as keyword arguments.

这篇关于为什么我的烧瓶表格验证返回不是有效选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆