WTForms:测试是否填写字段 [英] WTForms: test whether field is filled out
问题描述
我在WTForms中遇到了一个我认为很简单的任务,即遇到麻烦:检查某个字段是否在其中输入了任何数据.似乎该数据方法根据字段的类型返回不同的结果,因此我不能泛泛地使用它来对此进行测试.
I'm having trouble with what I thought would be a very simple task in WTForms: checking to see if a field has had any data entered in it. It seems that the data method returns different results depending on the type of field, so I can't use it generically to test this.
我正在Flask应用程序中使用它.在我的表单声明中,我有:
I'm using this in a Flask app. In my forms declaration, I have:
title = StringField('Title')
signed = RadioField('Signed?', choices = [('Y','yes'),('N','no')])
然后,在我的views.py中,在"if form.is_submitted()"之后:
Then, in my views.py, after an "if form.is_submitted()":
print('Title raw_data is %r' % form.title.raw_data)
print('Title data is %r' % form.title.data)
if form.title.raw_data:
print ("Title raw data is True")
if form.title.data:
print ("Title data is True")
print('Signed raw_data is %r' % form.signed.raw_data)
print('Signed data is %r' % form.signed.data)
if form.signed.raw_data:
print ("Signed raw data is True")
if form.signed.data:
print ("Signed data is True")
运行此命令并填写表单中的任何字段时,我得到:
When I run this and fill out neither field in the form, I get:
Title raw_data is ['']
Title data is ''
Title raw data is True
Signed raw_data is []
Signed data is 'None'
Signed data is True
即form.title.data返回空字符串,因此我可以将其用于测试;form.signed.data返回字符串"None",而不是None,因此它无法通过"if form.signed.data"测试.因此,我必须使用form.signed.raw_data,它返回一个空列表,即False.但是我不能在所有实例中都使用raw_data,因为form.title.raw_data不会返回空列表,它会返回由空字符串组成的列表,该字符串为 not True
That is, form.title.data returns the empty string, so I can use this for a test; form.signed.data returns the string 'None', rather than None, so it fails the "if form.signed.data" test. Thus I have to use form.signed.raw_data, which returns an empty list, which is False. But I can't use raw_data in all instances, because form.title.raw_data doesn't return an empty list, it returns a list consisting of the empty string, which is not True
有没有一种方法可以测试是否填写了任何类型的字段,或者我必须根据字段的类型在数据和raw_data之间切换?这是违反直觉和令人困惑的.
Is there a way I can test if any kind of field is filled out, or do I have to switch between data and raw_data depending on the kind of field? This is counterintuitive and confusing.
推荐答案
In raw_data
, values for a form field are collected in a list.
You want to work with data
because its value is processed to its python representation. Most fields subclassing wtforms.fields.core.Field
implements their own process_formdata
method which fulfills this.
首先实现一个帮助程序功能,该功能可以像这样检查字段数据:
Start by implementing a helper function that checks field data like so:
def is_filled(data):
if data == None:
return False
if data == '':
return False
if data == []:
return False
return True
请注意,如果在字段模式中指定了默认值,则数据会开始设置为默认值.在这种情况下,使用 raw_data
更为正确.
Note that data starts out set to the default value if a default value is specified in the field schema. In this case using raw_data
is more correct.
def is_filled(raw_data):
try:
value = raw_data[0]
if value == '':
return False
except (IndexError, TypeError):
return False
return True
测试:
>>> is_filled(None)
False
>>> is_filled([])
False
>>> is_filled(['',])
False
这篇关于WTForms:测试是否填写字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!