{% for subfield in form.time_o...">

Default Values ​​for WFForms RadioField

I create an html form with wtforms as follows:

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

My form class is as follows:

 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 open the editing form now, I get the value 1 or 2 through SQL - how can I program the radio specification?

+6
source share
3 answers

default = 2 should 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()]) 
+5
source

If I understand your question correctly, you want to have a form visualized with a pre-selected selection (instead of returning the default selection if not submitted to the form) ...

What you can do is build the form by setting a pre-selected value:

 myform = SN4639(time_offset='2') 

And then pass myform to your template to be displayed.

+2
source

Form.__init__ accepts the keyword argument obj= , which fills the form from this object if form data or other default values ​​are not specified. Pass the result from the database to this, and it should work.

0
source

Source: https://habr.com/ru/post/945595/


All Articles