I need to upload an image through a form. I also need to have the
ability to use the "clear" checkbox built into the imagefield widget
to have the image reference in the database removed. Images should
also be checked for filetype, dimensions and rejected if appropriate.
So I did this:
def clean_image(self):
image = self.cleaned_data['image']
if image:
from django.core.files.images import get_image_dimensions
w, h = get_image_dimensions(image)
if not image.content_type in settings.VALID_IMAGE_FORMATS:
raise forms.ValidationError(u'Only *.gif, *.jpg and
*.png images are allowed.')
if w > settings.VALID_IMAGE_WIDTH or h >
settings.VALID_IMAGE_HEIGHT:
raise forms.ValidationError(u'That image is too big.
The image needs to be ' + str(settings.VALID_IMAGE_WIDTH) + 'px * ' +
str(settings.VALID_IMAGE_HEIGHT) + 'px (or less).')
return image
This does all of what I need it to do, however when I use the clear
checkbox nothing happens. The form just validates and appears to be
successful. The database reference doesn't get removed.
If I remove the code above from my forms.py the checkbox works but I
then miss out on my dimension checking.
Am I missing something please?
Thank you
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment