Reputation: 403
I have the following:
forms.py:
student_account = BooleanField('student_account')
index.html
{{ form.student_account.label() }} {{ form.student_account(class="form-control") }}
And this code results into the following html:
<input id="student_account" name="student_account" type="checkbox" value="y">
How can I set the value to be 1 instead of 'y'? Why in fact all of the sudden, there is a 'y' as a value?
Upvotes: 0
Views: 1728
Reputation: 55630
'y' is the default value for Boolean inputs' value
attribute in wtforms.
According to the docstring for the base Input widget
By default, the
_value()
method will be called upon the associated field to provide thevalue=
HTML attribute.
And BooleanField
's _value method is
def _value(self):
if self.raw_data:
return text_type(self.raw_data[0])
else:
return "y"
If you really want a different value you could override it by setting it in the field's render_kw
argument:
foo = BooleanField('bar', render_kw={'value': '1'})
or by subclassing BooleanField
and overriding its _value
method.
Usually there isn't much benefit in overriding this value; wtforms will automatically cast the value to a boolean when form.data
is constructed.
from wtforms import Form, BooleanField
from werkzeug.datastructures import MultiDict
class MyForm(Form):
b = BooleanField('MyBool')
f = MyForm(formdata=MultiDict({'b': 'y'})
f.data
{'b': True}
f = MyForm(formdata=MultiDict({})
f.data
{'b': False}
Upvotes: 2