简体   繁体   English

Python:Pypugjs无线电字段中的Wtforms无法正常工作

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

I'm building a form with wtforms. 我正在用wtforms构建表单。 There is a radio field. 有一个无线电场。 Here is its data: 这是它的数据:

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

I render the field in pypugjs: 我在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. 提交后,我得到了Not a valid choice错误。 But after changing choices to string value (1->'1') 但是在将选项更改为字符串值(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 ? 现在它可以与Wtforms验证一起使用,所以这意味着我必须在RadioField使用字符串作为单选值?

Then there is the new trouble: I couldn't get pypugjs field checked even after use condition to check equal value 然后是新的麻烦:即使在使用条件检查相等值后,我也无法检查pypugjs字段

if record_data[key]==option_key
    |matched

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

So it means pypugjs doesn't match two same values, because one is Integer, one is String ?! 所以这意味着pypugjs不匹配两个相同的值,因为一个是Integer,一个是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. RadioField类采用coerce参数,该参数定义了一个函数,该函数应用于POST请求中接收的值。 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. RadioField的默认coerce功能是unicode ,因此该字段的值是一个字符串,但是您可以改用int以获得整数。

class Foo(wtforms.Form):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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