Hi,
> I have an image upload form and I want to INvalidate the form if the
> uploaded image it too big. How can I get an error message in the form
> saying the uploaded image is too big?
>
> ===Model==================
> class Image(models.Model):
> image = models.ImageField(upload_to='imageupload')
>
> ===View===================
> if imageForm.is_valid():
> inst = imageForm.save(commit=False)
> if inst.image.width > 640:
> #TODO: set the error message of this form to 'image is too
> big'
It's much simpler (and more appropriate) to do this in your form
itself. Specifically, add a clean_image method to your form and check
the image dimensions there. Then raise a forms.ValidationError on the
appropriate condition. Something along these lines would be a start:
def clean_image(self):
from django.core.files.images import get_image_dimensions
image = self.cleaned_data['image']
w, h = get_image_dimensions(image)
if w > 640:
raise forms.ValidationError(u'That image is too wide.')
return image
-Rajesh D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---