class UploadPicture forms Form description forms CharField required Fa

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class UploadPicture(forms.Form):
description = forms.CharField(required=False, widget=forms.Textarea())
picture = forms.ImageField()
def clean_picture(self):
raw_content = StringIO(self.cleaned_data['picture'].content)
image = Image.open(raw_content)
if image.format not in ['JPEG', 'PNG']:
raise forms.ValidationError(u'Only jpg and png files accepted')
return self.cleaned_data