如何在Flask中制作RadioField? [英] How to make a RadioField in Flask?

查看:899
本文介绍了如何在Flask中制作RadioField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有TextField,FileField的表单,并且我想添加一个RadioField.

I have a form with a TextField, FileField, and I want to add a RadioField.

我想要一个带有两个选项的无线电字段,用户只能在其中选择一个.我正在使用前两种形式的示例.

I'd like to have a radio field with two options, where the user can only select one. I'm following the example of the two previous forms that work.

我的forms.py看起来像这样

My forms.py looks like this

    from flask import Flask, request
    from werkzeug import secure_filename
    from flask.ext.wtf import Form, TextField, BooleanField, FileField, file_required,         RadioField
    from flask.ext.wtf import Required
    class ImageForm(Form):
        name = TextField('name', validators = [Required()])
        fileName = FileField('fileName', validators=[file_required()])
        certification = RadioField('certification', choices = ['option1', 'option2'])

我的views.py文件中有

In my views.py file I have

form = myForm()
if form.validate_on_submit():
    name = form.name.data
    fileName = secure_filename(form.fileName.file.filename)
    certification = form.certification.data

我的.html文件中有

In my .html file I have

     {% block content %}
     <h1>Simple Form</h1>
     <form action="" method="post" name="simple" enctype="multipart/form-data">
         {{form.hidden_tag()}}
         <p>
             Name:
             {{form.name(size=80)}}
         </p>
         <p>
             Upload a file
             {{form.fileName()}}
         </p>
         <p>
             Certification:
             {{form.certification()}}
         </p>
         <p><input type="submit" value="Submit"></p>
     </form>
     {% endblock %}

我似乎无法使用单选按钮表单在线找到某人的示例.我在 http://wtforms.simplecodes.com/docs/0.6/中找到了RadioField的描述. fields.html

I can't seem to find examples online of someone using a radio button form. I found a description of RadioField here http://wtforms.simplecodes.com/docs/0.6/fields.html

当我尝试访问我的表单页面时,出现DEBUG错误" ValueError:太多值无法解包"

When I try to visit the my form page I get the DEBUG error "ValueError: too many values to unpack"

推荐答案

在forms.py中,RadioField必须看起来像这样

In the forms.py the RadioField needs to look like this

    RadioField('Label', choices=[('value','description'),('value_two','whatever')])

其中的选项分别是描述"和任何",提交的值分别是值"和值两个".

Where the options are 'description' and 'whatever' with the submitted values being 'value' an 'value_two' respectively.

这篇关于如何在Flask中制作RadioField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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