Skip to content

Instantly share code, notes, and snippets.

@doobeh
Forked from anonymous/siecje.py
Last active April 19, 2022 10:25
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save doobeh/4667330 to your computer and use it in GitHub Desktop.
Save doobeh/4667330 to your computer and use it in GitHub Desktop.
Simple example of using a RadioField in Flask-WTForms to generate a form.
<form method="post">
{{ form.hidden_tag() }}
{{ form.example }}
<input type="submit">
</form>
from flask import Flask, render_template
from flask.ext.wtf import Form, RadioField
SECRET_KEY = 'development'
app = Flask(__name__)
app.config.from_object(__name__)
class SimpleForm(Form):
example = RadioField('Label', choices=[('value','description'),('value_two','whatever')])
@app.route('/',methods=['post','get'])
def hello_world():
form = SimpleForm()
if form.validate_on_submit():
print form.example.data
else:
print form.errors
return render_template('example.html',form=form)
if __name__ == '__main__':
app.run(debug=True)
@xaratustrah
Copy link

This is how it worked for me under python 3.

from flask import Flask, render_template
from wtforms import Form, RadioField

SECRET_KEY = 'development'

app = Flask(__name__)
app.config.from_object(__name__)


class SimpleForm(Form):
    example = RadioField(
        'Label', choices=[('value', 'description'), ('value_two', 'whatever')])


@app.route('/', methods=['post', 'get'])
def hello_world():
    form = SimpleForm()
    if form.validate():
        print(form.example.data)
    else:
        print(form.errors)
    return render_template('example.html', form=form)


if __name__ == '__main__':
    app.run(debug=True)

I also needed to remove the hidden_tag() from the form, since I get the error message: object has no attribute 'hidden_tag'.

<form method="post">
  {{ form.example }}
    <input type="submit">
</form>

@RobertDeRose
Copy link

This was what I had to do to get it working on Python 3 using FlaskForm

from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import RadioField


SECRET_KEY = 'development'


app = Flask(__name__)
app.config.from_object(__name__)
class SimpleForm(FlaskForm):
    example = RadioField('Label', choices=[
        (1,'description'), (2,'whatever')],
        default=1, coerce=int)


@app.route('/',methods=['post','get'])
def hello_world():
    form = SimpleForm()
    if form.validate_on_submit():
        print(form.example.data)
    else:
        print(form.errors)
    return render_template('example.html',form=form)


if __name__ == '__main__':
    app.run(debug=True)

@salokesh99
Copy link

this works

@tejad245
Copy link

how to validate the radiobutton

@yuliuftd
Copy link

yuliuftd commented Oct 5, 2020

how to make the options side by side, like this:

  • option 1 *option 2 *option3 * option4 ...

@rafzei
Copy link

rafzei commented Apr 19, 2022

how to validate the radiobutton

from wtforms.validators import InputRequired

example = RadioField('Label', choices=[
        (1,'description'), (2,'whatever')],
        default=1, coerce=int, validators=[InputRequired()])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment