Django - form validate images on upload

How to clean() an image with his size and extension? Image is uploading well into db, but when i'm trying to get cleaned_data it returns None.

I set same looking validator on models and it works well, but only when i upload an image by adminsite.

forms.py:

class AdditionalInfoForm(forms.Form):
first_name = forms.CharField(label='First name', max_length=20)
last_name = forms.CharField(label='Last name', max_length=40)
male = forms.ChoiceField(label="Male", choices=MALES, required=False)
date_of_birth = forms.DateTimeField(label="Date of birth", required=False, widget=DateInput)
about = forms.CharField(label='Tell us about yourself', widget=forms.Textarea(), required=False)
image = forms.ImageField(label="Profile picture", required=False)

def clean_image(self):
    avaible_formats = [
        'png',
        'jpeg',
        'jpg',
        ]
    img = self.cleaned_data.get('image')
    print(img) #always None
    if img:
        img_ext = img.name.split(".")[-1]
        if img.size > 4194304:
            raise ValidationError("Image size must be less than 4MB")
        if img_ext not in avaible_formats:
            raise ValidationError("Image extension is not avaible")        
    return img
Back to Top