Flask-WTF 表单填充整数字段和单选字段

2 投票
2 回答
4445 浏览
提问于 2025-04-18 05:46

我正在尝试填写一个完整的表单,前提是其中一个字段显示“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'>

输出是完全相同的,变量赋值是有效的,但我在显示的表单中看不到这些值。

2 个回答

0

你可以试试这个:

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

wtforms 需要你提供正确的数据类型的值

3

我试着重现你遇到的问题,但我没有看到。你可能会觉得我在使用 RadioField 时的结果很有趣,我可以给你指个方向,告诉你一个有效的解决方案。

这是你问题的关键代码:

form.publishInCollegeCatalog.data = True# 不起作用

这里的简单解决办法是这样做:

form.publishInCollegeCatalog.data = str(True)# 起作用

你把 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 = 字符串

给出输入元素的默认值。

规范中的 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">

这就是人们所期待的结果。

撰写回答