How to make RadioField in a flask?

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

I would like to have a radio field with two parameters where the user can select only one. I follow the example of the two previous forms that work.

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']) 

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 

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 %} 

I cannot find examples on the Internet from someone using a switch form. I found a description of RadioField here http://wtforms.simplecodes.com/docs/0.6/fields.html

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

+4
source share
2 answers

In .py forms, RadioField should look like this:

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

If the parameters are “description” and “whatever”, with the values ​​represented, “value” and “value_two”, respectively.

+15
source

form.certification () will not work. You need to iterate over the values ​​in the template:

Replace:

 {{ form.certification() }} 

with:

 {% for subfield in form.certification %} <tr> <td>{{ subfield }}</td> <td>{{ subfield.label }}</td> </tr> {% endfor %} 
+2
source

All Articles