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
Asked
Active
Viewed 503 times
0
-
1Have 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 Answers
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
-
-
You can use js to do this, e. g. http://jquerybyexample.blogspot.com/2012/03/how-to-check-file-size-before-uploading.html – szaman Apr 30 '13 at 05:11
-
the image does not have attribute "width" ( Anyway, I did it using PIL – moriesta Apr 30 '13 at 11:06