简体   繁体   中英

Python: Wtforms in pypugjs radio field doesn't work correctly

I'm building a form with wtforms. There is a radio field. Here is its data:

[(0, 'Active'),(1, 'Inactive'),]

I render the field in pypugjs:

input(type='radio', name=key, value=option_key, checked=(record_data[key]==option_key))=option_value

After the submitssion, i got Not a valid choice error. But after changing choices to string value (1->'1')

[('0', 'Active'),('1', 'Inactive'),]

Now it works with Wtforms validation, so it means i have to use string as radio value in RadioField ?

Then there is the new trouble: I couldn't get pypugjs field checked even after use condition to check equal value

if record_data[key]==option_key
    |matched

It doesn't work even the result of |#{record_data[key]}-#{option_key}- is 1-1-

So it means pypugjs doesn't match two same values, because one is Integer, one is String ?!

How do i get it work?

The RadioField class takes a coerce parameter which defines a function that is applied to the value received in the POST request. The default coerce function for RadioField is unicode , so the field's value is a string, but you can use int instead so that you get an integer.

class Foo(wtforms.Form):

    bar = wtforms.RadioField(coerce=int, choices=[(0, 'active'), (1, 'inactive')])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM