本文介绍了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:

如何将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:

从文档中:

在表单构建过程中将使用通过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跳过对字符串值的强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 22:30