Much nicer. Thanks so much Mike ...
d On Aug 24, 12:17 pm, Mike Ramirez <gufym...@gmail.com> wrote: > On Sunday 23 August 2009 05:36:13 pm The Danny Bos wrote: > > > > > > > Hey there, I've got the below code. > > Can someone help me fill in the gaps to rename the image to the > > 'SLUG'? > > > In the end I'm hoping for images like so: > > > ...com/books/book-title.jpg > > ...com/books/thumbs/book-title.jpg > > > Here's my save() definition from models.py: > > > def save(self): > > super(Book, self).save() > > > image_path = '%s%s' % (settings.MEDIA_ROOT, str(self.cover)) > > path, filename = os.path.split(image_path) > > > thumb_name = '%s/thumbs/%s' % (path, filename) > > > gen_thumb(image_path, thumb_name, size=160) > > self.thumb = '%s' % filename > > > super(Book, self).save() > > Try this: > > def gen_thumb(image, size=160): > save_path = os.path.join(settings.MEDIA_ROOT, image) > # gen thumb code here > image.save(save_path) > return > > def save(self): > thumb_path = os.path.join('books', 'thumbs', 'tn_%s' %(str(self.cover))) > gen_thumb(thumb_path): > self.thumb = thumb_path > super(Book, self).save() > > First off, you should use os.path.join() when creating a file path; this will > allow your code to be os independant as os.path.join() will set the right > path seperators for the os. (Just a nit picky thing for os independance). > > You should set the ImageField to a path relative to your settings.MEDIA_ROOT > setting, the ImageField will automatically join[1] these together for use > with thumb.path and you'll be able to use thumb.url for you img html tags > (i.e. <img src="{{ MEDIA_URL }}{{ book.thumb.url }}" alt="{{ book.title }}" > width="book.thumb.width" height="book.thumb.height" /> ) since the url is > actually the path relative to the MEDIA_ROOT/MEDIA_URL. > > You can alter the gen_thumb to return the relative path and use it in the > upload_to= option of the ImageField and save yourself the headache, but the > gen_thumb should follow the instructions in handling uploaded files[2]. > > Mike > > [1]http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield > [2]http://docs.djangoproject.com/en/dev/topics/http/file-uploads/#handli... > -- > The trouble is, there is an endless supply of White Men, but there has > always been a limited number of Human Beings. > -- Little Big Man > > signature.asc > < 1KViewDownload --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---