WTForms RadioField default values

Kilrathy picture Kilrathy · May 22, 2013 · Viewed 9.1k times · Source

I'm generating a html form with wtforms like this:

<div class="control-group">
    {% for subfield in form.time_offset %}
    <label class="radio">
        {{ subfield }}
        {{ subfield.label }}
    </label>
    {% endfor %}
</div>

My form class is like this:

class SN4639(Form):
    time_offset = RadioField(u'Label', choices=[
        ('2', u'Check when Daylight saving has begun, UTC+02:00'),
        ('1', u'Check when Daylight saving has stopped, UTC+01:00')],
        default=2, validators=[Required()])

When I now open the edit form, I get via SQL the value 1 or 2 - how can I preset the specifiy radiobutton?

Answer

sparker picture sparker · Dec 7, 2014

default=2 needs to be of type string, not int:

class SN4639(Form):
    time_offset = RadioField(u'Label', choices=[
        ('2', u'Check when Daylight saving has begun, UTC+02:00'),
        ('1', u'Check when Daylight saving has stopped, UTC+01:00')],
        default='2', validators=[Required()])