WTForms RadioField default values
Asked Answered
S

4

17

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?

Shrike answered 22/5, 2013 at 14:14 Comment(5)
If you want to set default at forms for which reasons are you want to do that in html? Do you want two default values?Insurer
At the creation form, I set defaults, as you can see, in the class. But now I'm creating a edit form and there it should be pre-selected. e.g. "male / female" and when I got the value 'm' from the db, male should be selected... How can I do this?Shrike
default=yourObjectFromDB.sex ????Insurer
Hi, sorry, maybe I'm to new or dump... and don't get it. Do you set this in the class or in the template? However, looks like I'll keep trying this weekend.Shrike
lel, I understand you set class as usually but attache _ at the end that's because "class" is a reserved word. So you can set class in templates like this class_="someclass"Insurer
L
12

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()])
Levant answered 7/12, 2014 at 22:16 Comment(0)
S
3

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.

Stadiometer answered 30/6, 2013 at 3:22 Comment(0)
G
0

My friend,

You need to fill in the .data field with the data that is in the database after instantiating the form. See that example:

my_instance = SN4639()
my_db_id = get_id_on_my_db()  # fill the variable with the database id

my_instance.time_offset.data = my_db_id  # here's the cat jump

Now, when you use my_instance.time_offset () in your HTML, the correct radio will be selected

IMPORTANTE NOTE:

If your id is an integer, you must add the parameter coerce=int in RadioField. See that example:

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')],
    coerce=int,
    default=2, validators=[Required()])
Gerrygerrymander answered 20/4, 2021 at 15:40 Comment(1)
almost 1 year later i am feeling sad because i replied like this.Gerrygerrymander
P
-1

Form.__init__ takes a keyword argument obj= which will populate the form from the given object if no formdata or other defaults are provided. Pass the result from the database to that and it should work.

Paquette answered 30/6, 2013 at 3:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.