0

How do say to ImageField that maximum allowed width is 500 and only *.JPGs are allowed? If it is not possible to do it in ImageField, what are the ways to implement it? Or should it be specified on the html form? Thanks in advance

moriesta
  • 1,170
  • 4
  • 19
  • 38
  • 1
    Have you looked at http://stackoverflow.com/questions/6195478/max-image-size-on-file-upload, http://stackoverflow.com/questions/2472422/django-file-upload-size-limit – Rohan Apr 29 '13 at 05:52

1 Answers1

1

You can check this in form clean methods:

def clean_picture(self):
    # check image weight
    image = self.cleaned_data['picture']
    if image:
        if image.width > 100:
            raise forms.ValidationError(_("Selected image is too wide"))
    return image

There is also height attribute. You can access name of the file in ImageField, get extension from it and validate it like I did with width.

szaman
  • 6,666
  • 13
  • 53
  • 81