Reputation: 333
I'm trying to populate a full form under the condition that one of the field says "MATH& 142". this appears to work for some fields and not others.
def formFill():
@app.route('/formFill', methods=['GET', 'POST'])
form = cifForm()
if form.courseNum.data == 'MATH& 142':
form.courseTitle.data = 'Precalculus II : Trigonometry'
form.publishInCollegeCatalog.data = True #NOT WORKING - Radio Field
form.numCredit.data = int(5) #NOT WORKING - Integer Field
class cifForm(Form):
courseTitle = StringField('Course Title')
publishInCollegeCatalog = RadioField('Publish in college catalog?', choices=[(True,'Yes'),(False,'No')], )
numCredit = IntegerField('Credits')
submit = SubmitField('Submit')
@app.route('/formFill', methods=['GET', 'POST'])
def formFill():
form = cifForm()
if form.courseNum.data == 'MATH& 142':
form.courseTitle.data = 'Precalculus II : Trigonometry'
form.publishInCollegeCatalog.data = True# NOT WORKING
print form.numCredit.data
print type(form.numCredit.data)
print form.creditIsVariable.data
print type(form.numCredit.data)
console:
5
<type 'int'>
False
<type 'int'>
@app.route('/formFill', methods=['GET', 'POST'])
def formFill():
form = cifForm()
if form.courseNum.data == 'MATH& 142':
form.courseTitle.data = 'Precalculus II : Trigonometry'
form.publishInCollegeCatalog.data = True# NOT WORKING
form.numCredit.data = int(5)
print form.numCredit.data
print type(form.numCredit.data)
form.creditIsVariable.data = bool(False) #: NOT WORKING
print form.creditIsVariable.data
print type(form.numCredit.data)
console:
5
<type 'int'>
False
<type 'int'>
The output is identical, the variable assignments work, but I don't see these values in the rendered form.
Upvotes: 2
Views: 4521
Reputation: 15363
I have tried to recreate your problem, but I am just not seeing it. You might however find my results working with RadioField
interesting and I can at least point you towards a working solution.
form.publishInCollegeCatalog.data = True# NOT WORKING
The simple fix here is to simply do something like this:
form.publishInCollegeCatalog.data = str(True)# WORKING
You are conflating True
with 'True'
:
from collections import namedtuple
from wtforms.validators import Required
from wtforms import Form
from wtforms import RadioField
from webob.multidict import MultiDict
class SimpleForm(Form):
example = RadioField('Label',
choices=[(True,'Truthy'),(False,'Falsey')])
# when this data is processed True is coerced to its
# string representation 'True'
data = {'example': True}
form = SimpleForm(data=MultiDict(data))
# checking form.data here yields - {'example': u'True'}
# This prints the radio markup using the value `True`
print form.example
form.example.data = True
# This prints the radio using the value True
print form.example
The RadioField
renders a string representation of the value
part of the choice. This is right in line with the HTML specification. Treated here.
value = string
Gives the default value of the input element.
The value
part of the spec is treated as a string to support a wide range of values. WTForms has no choice but to treat this type as a string when it does its processing step. Its the least common denominator.
Once again this is not a problem that I am able to recreate. Every single IntegerField
I create behaves exactly the same way. If you follow the example below your results should be the same.
class SimpleForm(Form):
my_int = IntegerField()
data = {'my_int': int(5)}
form = SimpleForm(data=MultiDict(data))
print form.my_int
form.my_int.data = int(6)
print form.my_int
<input id="my_int" name="my_int" type="text" value="5">
<input id="my_int" name="my_int" type="text" value="6">
which is what one would expect.
Upvotes: 3
Reputation: 46
You could try using:
form.numCredit.data = int(5) # This is defined as integer in your form
form.creditIsVariable.data = bool(False) # and so on
wtforms expects values in the proper datatype
Upvotes: 0