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
1 Answer
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.
3 Comments
moriesta
is it possible to check this before uploading?
szaman
You can use js to do this, e. g. jquerybyexample.blogspot.com/2012/03/…
moriesta
the image does not have attribute "width" ( Anyway, I did it using PIL