I have writed this code but it doesn't work. What is the error ?

from django.db import models
from django.forms import ModelForm
from PIL import Image
import glob, os

thumb_size = 90, 90

class book(models.Model):
    title = models.CharField(max_length=200)
    photo = models.ImageField(upload_to='bookphoto')
    thumb = models.ImageField(upload_to='bookthumb')

    def save(self):

        file_path = self.photo.path
        if (file_path):
            imfile, ext = os.path.splitext(file_path)
            im = Image.open(file_path)
            im.thumbnail(thumb_size, Image.ANTIALIAS)
            im.save(imfile + ".thumbnail.jpg", "JPEG")
            self.thumb = imfile + ".thumbnail.jpg"
        super(book, self).save()

class BookForm(ModelForm):

    class Meta:
        model = book
        exclude = ('thumb',)

Please help me, I am going crazy :-\



--------------

On Jan 2, 5:17 pm, Xia Kai(夏恺) <xia...@gmail.com> wrote:
> Hi,
>
> This is a fantastic app, though it might be too fat for a minimalist like
> me.   ^_^
>
> I would recommend override the default save method of the model and resize
> the original photo using PIL. For the overriding part, you could consult the
> documentation:http://docs.djangoproject.com/en/dev/topics/db/models/#overriding-pre...
>
> ------------------------
> Xia Kai(夏恺)
> xia...@gmail.comhttp://blog.xiaket.org
>
> --------------------------------------------------
> From: "Chris Moffitt" <ch...@moffitts.net>
> Sent: Saturday, January 02, 2010 11:59 PM
> To: <django-users@googlegroups.com>
> Subject: Re: Photo + thumbnail
>
> > You'll probably want to use one of Django's thumbnail apps. Here's the one
> > I
> > recommend:
> >http://code.google.com/p/sorl-thumbnail/*
>
> > -*Chris
>
> > On Sat, Jan 2, 2010 at 9:48 AM, nameless <xsatelli...@gmail.com> wrote:
>
> >> Hi everyone I have a simple question.
> >> This is my model:
>
> >> class book(models.Model):
> >>    title = models.CharField(max_length=50)
> >>    photo = models.ImageField(upload_to='images/avatar/')
> >>    thumb = models.ImageField(upload_to='images/thumb/')
>
> >> I want in photo original photo and in thumb the same photo but
> >> resized.
> >> How do I do that in simplest way ?
>
> >> Thank you and Good year  ^_^

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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