I am using radio field, and I would like the default value to be rendered as (.) instead of ( ) . I tried straightforward approach:
choice_switcher = RadioField('Choice?', [validators.Required()], choices=[('choice1', 'Choice One'),('choice2', 'Choice Two')], default='choice1')
It didn't work. It renders two choices as:
( ) Choice One
( ) Choice Two
Whereas I'd like to see this:
(.) Choice one
( ) Choice Two
Works alright for me:
from flask import Flask, render_template
from flask_wtf import Form
from wtforms import validators, RadioField
app = Flask(__name__)
app.secret_key = 'TEST'
class TestForm(Form):
choice_switcher = RadioField(
'Choice?',
[validators.Required()],
choices=[('choice1', 'Choice One'), ('choice2', 'Choice Two')], default='choice1'
)
@app.route('/')
def hello_world():
testform = TestForm()
return render_template('test_form.html', form=testform)
if __name__ == '__main__':
app.run(debug=True)
{{ form.choice_switcher() }}
<ul id="choice_switcher">
<li><input checked id="choice_switcher-0" name="choice_switcher" type="radio" value="choice1"> <label
for="choice_switcher-0">Choice One</label></li>
<li><input id="choice_switcher-1" name="choice_switcher" type="radio" value="choice2"> <label
for="choice_switcher-1">Choice Two</label></li>
</ul>
If you're coming to this question with the same problem six years later, like I did, you might check whether you have accidentally set coerce=int
on the RadioField in your form's class. This fails silently except for refusing to honour the default specified either in the form class (e.g. default=3
), or the default you specify in the route from the user's data. Hope this helps someone some day.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With