WTforms:错误 “域不存在”
问题描述:
您好我有一个窗体类看起来象下面这样: -WTforms:错误 “域不存在”
class UserCreateForm(wtf.Form):
name=wtf.TextField('Name',validators=[validators.Required(),username_check])
email=wtf.TextField('Email')
userimage=wtf.FileField(u'Upload Image',validators=[checkfile])
自定义验证功能 “checkfile” 看起来是这样的: -
def checkfile(form,field):
if field.data:
filename=field.data.lower()
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
if not ('.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS):
raise ValidationError('Wrong Filetype, you can upload only png,jpg,jpeg,gif files')
else:
raise ValidationError('field not Present') # I added this justfor some debugging.
然而我发现,即使我浏览模板中的文件,然后单击提交,它总是会引发“字段不存在”的错误。我是一个 这里有点困惑。是field.data不检查 名
答
解决了这个最后的存在以正确的方式,曾与field.file
更换field.data
的验证,然后访问使用field.file.filename
其属性。
您应该将此标记为接受的答案。 – 2010-11-13 02:30:07