开发者

WTforms: Error "field not present"

Hi I have a form class which looks like below:-

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])

The custom validator function " checkfile" looks like this:-

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.

However I find that even though I browse a file in the template and click submit , it always raises the error "field not present". I am a little confused here . Is field.data not the right way to check for the presence of filenames


Solved this finally , had to replace field.data in the validator with field.file and then access its attributes using field.file.filename.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜