【问题标题】:Flask-WTF form populating integer fields and radio fieldsFlask-WTF 表单填充整数字段和单选字段
【发布时间】:2014-05-08 03:03:55
【问题描述】:

我正在尝试在其中一个字段显示“MATH&142”的情况下填充完整的表单。这似乎适用于某些领域,而不适用于其他领域。

代码如下:

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)

控制台:

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)

控制台:

5
<type 'int'>
False
<type 'int'>

输出是相同的,变量赋值有效,但我没有在呈现的表单中看到这些值。

【问题讨论】:

    标签: python flask jinja2 wtforms


    【解决方案1】:

    我试图重现您的问题,但我只是没有看到它。但是,您可能会发现我使用 RadioField 的结果很有趣,我至少可以为您指出一个可行的解决方案。

    这是你的问题行:

    form.publishInCollegeCatalog.data = True# NOT WORKING

    这里的简单解决方法是简单地做这样的事情:

    form.publishInCollegeCatalog.data = str(True)# WORKING

    您将True'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
    

    他们渲染什么?

    第一次打印:

    第二次打印:

    解释:

    RadioField 呈现选择的value 部分的字符串表示形式。这完全符合 HTML 规范。在这里处理。

    值 = 字符串

    给出输入元素的默认值。

    规范的value 部分被视为字符串以支持广泛的值。 WTForms 在处理步骤时别无选择,只能将此类型视为字符串。它是最小公分母。

    但是我的整数呢?

    再一次,这不是我能够重现的问题。我创建的每个IntegerField 的行为方式完全相同。如果你按照下面的例子,你的结果应该是一样的。

    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">
    

    这是人们所期望的。

    【讨论】:

      【解决方案2】:

      您可以尝试使用:

      form.numCredit.data = int(5) # This is defined as integer in your form
      form.creditIsVariable.data = bool(False) # and so on
      

      wtforms 需要正确数据类型的值

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多