2014-09-22 83 views
2

我在我的Django项目与化身领域的用户模型:如何使静态图像的缩略图使用SORL-缩略图

class User(AbstractBaseUser): 

    avatar = models.ImageField(
     null=True, 
     blank=True, 
     upload_to='user/avatar/', 
    ) 

不是必需的化身,所以我想用一个如果用户尚未上传默认图片。在另一方面,我不想为了使用default参数可以改变默认的头像在未来为所有用户:

class User(AbstractBaseUser): 

    avatar = models.ImageField(
     null=True, 
     blank=True, 
     default='defaults/no-avatar.png', 
     upload_to='user/avatar/', 
    ) 

所以我最后写一个get_avatar方法返回一个图像如果化身存在或默认的静态图像的路径:

@property 
def get_avatar(self): 
    if self.avatar: 
     return self.avatar 
    return '{0}defaults/no-avatar.png'.format(settings.STATIC_URL) 

但在这种情况下sorl-thumbnail不生成缩略图的默认图像

{% thumbnail user.get_avatar "46x46" crop="center" as im %} 
    <img title="{{ user }}" src="{{ im.url }}" class="img-circle" /> 
{% endthumbnail %} 

,并返回以下错误:

ERROR 2014-09-22 12:49:48,020 thumbnail :: Thumbnail tag failed: 
Traceback (most recent call last): 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/sorl/thumbnail/templatetags/thumbnail.py", line 45, in render 
    return self._render(context) 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/sorl/thumbnail/templatetags/thumbnail.py", line 97, in _render 
    file_, geometry, **options 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/sorl/thumbnail/base.py", line 56, in get_thumbnail 
    source_image = default.engine.get_image(source) 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/sorl/thumbnail/engines/pil_engine.py", line 12, in get_image 
    buf = StringIO(source.read()) 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/sorl/thumbnail/images.py", line 121, in read 
    return self.storage.open(self.name).read() 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/files/storage.py", line 33, in open 
    return self._open(name, mode) 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/files/storage.py", line 159, in _open 
    return File(open(self.path(name), mode)) 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/files/storage.py", line 260, in path 
    raise SuspiciousFileOperation("Attempted access to '%s' denied." % name) 

我试图重写方法,以便返回而不是图像:

from django.core.files.images import ImageFile 

@property 
def get_avatar(self): 
    if self.avatar: 
     return self.avatar 
    return ImageFile(open(os.path.join(settings.STATIC_ROOT, 'defaults/no-avatar.png'), 'r')) 

,但我有一个类似的错误:

ERROR 2014-09-22 12:52:18,448 thumbnail :: Thumbnail tag failed: 
Traceback (most recent call last): 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/sorl/thumbnail/templatetags/thumbnail.py", line 45, in render 
    return self._render(context) 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/sorl/thumbnail/templatetags/thumbnail.py", line 97, in _render 
    file_, geometry, **options 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/sorl/thumbnail/base.py", line 56, in get_thumbnail 
    source_image = default.engine.get_image(source) 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/sorl/thumbnail/engines/pil_engine.py", line 12, in get_image 
    buf = StringIO(source.read()) 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/sorl/thumbnail/images.py", line 121, in read 
    return self.storage.open(self.name).read() 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/files/storage.py", line 33, in open 
    return self._open(name, mode) 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/files/storage.py", line 159, in _open 
    return File(open(self.path(name), mode)) 
    File "/Users/vera/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/files/storage.py", line 260, in path 
    raise SuspiciousFileOperation("Attempted access to '%s' denied." % name) 
SuspiciousFileOperation: Attempted access to '/Users/vera/workspace/my-website/static/defaults/no-avatar.png' denied. 

回答

2

sorl-thumbnail使用storage functionality。 Django将决定如何以及在何处将文件存储到file storage system。这是实际理解文件系统,打开和读取文件等事物的对象。Django附带了一个django.core.files.storage.FileSystemStorage类,它实现了基本的本地文件系统文件存储。默认情况下,Django使用MEDIA_ROOT和MEDIA_URL设置在本地存储文件。

所以在你的情况下,django试图在MEDIA_ROOT下找到图像,但是你已经将图像保存在STATIC_ROOT下。

如何修复

如修补程序可以MEDIA_ROOT下尝试移动图像,并会在get_avatar方法改变路径。 或者您可以尝试编写自定义存储,这将与这两个文件夹一起使用。 Example custom storage