Before I can write validation of uploaded images, I first need to know
how to save multiple upload images in view. Can anyone share some code
or reference on this? Thanks.

On Dec 20, 10:31 pm, Brandon Taylor <btaylordes...@gmail.com> wrote:
> Hi,
>
> You can do validation on images by using a custom form for your admin
> Model. This example validates that a thumbnail image is a specific
> dimension, file type and file size,
>
> from django.core.files.images import get_image_dimensions
>
> class MyModelAdminForm(forms.ModelForm):
>     model = MyModel
>
>     def clean_thumbnail(self):
>         thumbnail_image = self.cleaned_data['thumbnail']
>
>         try:
>             main, sub = thumbnail_image.content_type.split('/')
>             w, h = get_image_dimensions(thumbnail_image)
>
>             if w != 128 or h != 96:
>                 raise forms.ValidationError(u'Please use an image that
> is 128 x 96 pixels for thumbnails')
>
>             if not (main == 'image' and sub in ['jpeg', 'pjpeg']):
>                 raise forms.ValidationError(u'Please use a JPEG
> image.')
>
>             if len(thumbnail_image) > 20480: #bytes
>                 raise forms.ValidationError(u'Thumbnail file size must
> not exceed 20 KB.')
>
>         except AttributeError:
>             pass
>
>         return thumbnail_image
>
> class MyModelAdmin(admin.ModelAdmin):
>     form = MyModelAdminForm
>
> HTH,
> Brandon
>
> On Dec 20, 8:00 pm, Chuck22 <cba...@gmail.com> wrote:
>
>
>
> > Here is my model:
>
> > class Book(models.Model):
> >     title = models.CharField(max_length=150)
> >     category = models.CharField(max_length=50)
> >     description = models.CharField(max_length=2000)
> >     image1 = models.ImageField(upload_to='img/bk', blank=True)
> >     image2 = models.ImageField(upload_to='img/bk', blank=True)
> >     image3 = models.ImageField(upload_to='img/bk', blank=True)
>
> > I want to create a form to add/edit a book which can upload image
> > files (I want to make sure it is an image file and individual image
> > file is within size limit, e.g. 500kb, and dimension limit, e.g.
> > 800X600 px).
>
> > I don't think I can use ModelForm because this validation requirement,
> > also because I want to automatically set the category value for the
> > form on the fly. But don't know 1) how to save uploaded images in view
> > along with the new Model object. 2) how to do validation on the
> > uploaded image files.
>
> > I found a close reference:
>
> >http://www.oluyede.org/blog/2007/03/18/django-image-uploading-validat...
>
> > But it reads: "This post is outdated now - if you’re using the latest
> > django, do not follow this post." I don't know what to follow for
> > Django 1.0.2 that I am using. The Django document on 
> > forms:http://docs.djangoproject.com/en/dev/topics/forms/
> > does not even mention how to ceate a form for ImageField in Model. Can
> > anyone help with some code snippets? Thanks.- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to