WTForms:IntegerField跳过对字符串值的强制转换 [英] WTForms: IntegerField skips coercion on a string value

查看:82
本文介绍了WTForms:IntegerField跳过对字符串值的强制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有单个IntegerFieldForm实例.

I have a Form instance with a single IntegerField.

IntegerField与type="text"一起作为<input>呈现为HTML,并且数据以文本字符串的形式从HTML表单回发.但是,该表单将无法验证发布的数据是否具有IntegerField的字符串值(通过data参数中的dict传递).

The IntegerField renders to HTML as an <input> with type="text" and data gets POSTed back from an HTML form as a text string. However the form will not validate if the posted data has a string value for the IntegerField (passed in via a dict in the data parameter).

这是一个玩具示例:

from wtforms import validators, Form, IntegerField 

class TestForm(Form):
    num = IntegerField('How Many?', [validators.NumberRange(min=1, max=100)])


test_form1 = TestForm()
print("HTML Render 1: %s" % test_form1.num())

data_in = {'num': '66'}  # Note '66' is a string as would be POSTed
test_form2 = TestForm(data=data_in)
print("HTML Render 2: %s" % test_form2.num())
print("     Validate: %s" % test_form2.validate())
print("       Errors: %s" % test_form2.errors)

输出为:

HTML Render 1: <input id="num" name="num" type="text" value="">
HTML Render 2: <input id="num" name="num" type="text" value="66">
     Validate: False
       Errors: {'num': [u'Number must be between 1 and 100.']}

IntegerField的文档字符串说:

The docstring for IntegerField says:

IntegerField(Field):一个文本字段,除了所有输入都被强制为整数

IntegerField(Field): A text field, except all input is coerced to an integer

如何将str强制转换为int,以便此表单可以通过验证?

How can I coerce a str into an int such that this form will pass validation?

推荐答案

是来自其中一个 WTForms 开发人员:

This is from one of the WTForms devs:

字段仅强制转换表单数据,而不强制转换对象数据,这使人们可以使用像int一样"的对象,并且仍然可以使它们工作而不会破坏值.您有责任将正确的数据类型传递给对象/扭曲数据.

Fields only coerce form data, they don't coerce object data, this lets people use objects >"like an int" and still have them work without the value being clobbered. It's your >responsibility to pass correct datatypes to object/kwargs data.

从文档中:

process_formdata(值列表) 处理从表单通过网络接收的数据.

process_formdata(valuelist) Process data received over the wire from a form.

在表单构建过程中将使用通过formdata提供的数据来调用此方法 论点.

This will be called during form construction with data supplied through the formdata argument.

参数:valuelist –要处理的字符串列表.

Parameter: valuelist – A list of strings to process.

在您的示例中,将永远不会调用IntegerField上的process_formdata方法

In your example the process_formdata method on IntegerField will never be called

您传入的是str,这不会被强制,因为您将其作为data关键字参数提供. data关键字参数确切表示您要验证的数据,而无需强制.因为'66'仍然是str,所以验证程序不会让它通过.

You are passing in a str and this will not be coerced because you are supplying it as the data keyword argument. The data keyword argument signifies exactly the data you want to validate without coercion. Because '66' is still a str the validators won't let it pass.

formdata关键字参数指示数据从网络中传入.这将通过该字段的强制过程进行.只有一个陷阱,它仅接受MultiDict之类的对象.如果您看下面的示例,我使用了 webob MutliDict,但是 Werkzeug 库中还提供了一个.如果将常规的python字典包装在MultiDict中,并将其作为formdata关键字提供,则表单将按预期验证.

The formdata keyword argument indicates the data coming in off the wire. This will go through the field's coercion process. There is only one catch, it only accepts MultiDict like objects. If you look at the example below I've used the webob MutliDict but there is also one supplied in the Werkzeug library. If you wrap a regular python dictionary in a MultiDict and supply it as the formdata keyword your form will validate as expected.

from wtforms import validators, Form, IntegerField 
from webob.multidict import MultiDict

class TestForm(Form):
    num = IntegerField('How Many?', [validators.NumberRange(min=1, max=100)])

data_in = {'num': '66'}  # Note '66' is a string as would be POSTed
test_form2 = TestForm(formdata=MultiDict(data_in))
print("HTML Render 2: %s" % test_form2.num())
print("     Validate: %s" % test_form2.validate())
print("       Errors: %s" % test_form2.errors)


HTML Render 2: <input id="num" name="num" type="text" value="66">
     Validate: True
       Errors: {}

这篇关于WTForms:IntegerField跳过对字符串值的强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆