Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WTForms RadioField default values

Tags:

python

wtforms

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?

like image 476
Kilrathy Avatar asked May 22 '13 14:05

Kilrathy


People also ask

What does WTForms stand for?

WTForms is a flexible forms validation and rendering library for Python web development. It can work with whatever web framework and template engine you choose. It supports data validation, CSRF protection, internationalization (I18N), and more.

What is Stringfield?

String field theory (SFT) is a formalism in string theory in which the dynamics of relativistic strings is reformulated in the language of quantum field theory.


2 Answers

If I understand your question properly, you want to have the form render with a pre-selected choice (rather than returning a default choice if no value is submitted to the form)...

What you can do is construct the form while setting the pre-selected value:

myform = SN4639(time_offset='2')

And then pass myform off to your template to be rendered.

like image 26
Brandon W. King Avatar answered Sep 22 '22 08:09

Brandon W. King


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()])
like image 99
sparker Avatar answered Sep 20 '22 08:09

sparker