Аналогично ругаеться…
Exception Type: IOError
Exception Value:
(2, 'No such file or directory')
Exception Location: /usr/lib/python2.6/dist-packages/PIL/Image.py in open, line 1889
Python Executable: /usr/bin/python
Python Version: 2.6.2
Python Path:
19 строка кода image = Image.open(self.photo.name)
/home/xfree/Python/djcode/imagecms/../imagecms/cms/models.py in save
12. from django.core.files.uploadedfile import SimpleUploadedFile
13.
14. # Set our max thumbnail size in a tuple (max width, max height)
15. THUMBNAIL_SIZE = (50, 50)
16.
17. # Open original photo which we want to thumbnail using PIL's Image
18. # object
19. image = Image.open(self.photo.name)
Сам models.py
from django.db import models
from django.contrib import admin
class Photo(models.Model):
title = models.CharField(max_length=50)
photo = models.ImageField(upload_to="photos")
thumbnail = models.ImageField(upload_to="thumbnails/", editable=False)
def save(self):
from PIL import Image
from cStringIO import StringIO
from django.core.files.uploadedfile import SimpleUploadedFile
# Set our max thumbnail size in a tuple (max width, max height)
THUMBNAIL_SIZE = (50, 50)
# Open original photo which we want to thumbnail using PIL's Image
# object
image = Image.open(self.photo.name)
# Convert to RGB if necessary
# Thanks to Limodou on DjangoSnippets.org
# http://www.djangosnippets.org/snippets/20/
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
# We use our PIL Image object to create the thumbnail, which already
# has a thumbnail() convenience method that contrains proportions.
# Additionally, we use Image.ANTIALIAS to make the image look better.
# Without antialiasing the image pattern artifacts may result.
image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
# Save the thumbnail
temp_handle = StringIO()
image.save(temp_handle, 'png')
temp_handle.seek(0)
# Save to the thumbnail field
suf = SimpleUploadedFile(os.path.split(self.photo.name)[-1],
temp_handle.read(), content_type='image/png')
self.thunbnail.save(suf.name+'.png', suf, save=False)
# Save this photo instance
super(Photo, self).save()
class PhotoAdmin(admin.ModelAdmin):
list_display = ('title', 'photo', 'thumbnail')
search_fields = ('title', 'photo')
list_filter = ('title', 'photo')
admin.site.register(Photo, PhotoAdmin)
Убираю метод save() с модели, файл успешно сохраняеться в нужную директорию.