WTforms:错误“域不存在”
-
27-09-2019 - |
题
您好我有一个表格类看起来像下面: -
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.data
验证替换field.file
然后访问使用field.file.filename
其属性。
不隶属于 StackOverflow