嗨,我有一个表单类,如下所示:-
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 不是检查文件名是否存在的正确方法