清洁图像字段使Django中的表单无效

时间:2018-08-29 10:17:01

标签: django python-3.x django-forms

我正在尝试使用Pillow和django 2.1中以下代码清洁通过ImageField提交的照片。

    def clean_photo(self):
        photo = self.cleaned_data.get("photo")
        if photo:
            if photo.size > 100000:
                raise forms.ValidationError(u'Image is too large. Upload an image less than 100Kb in size')
            image = Image.open(photo)
            if image.size[0] > 250 or image.size[1] > 250:
                raise forms.ValidationError(u'Image is too large. Upload an image less than 250x250 dimensions')
        return photo

当我没有此方法时,表单可以正常工作,图片将被上传并保存。但是,使用这种方法时,我得到一个

    The UserForm could not be changed because the data didn't validate.

错误。感谢您提供有关为什么会发生这种情况的任何帮助。

谢谢大家

相同

0 个答案:

没有答案