我有 mediaservice 网站,我的文件存储在媒体文件夹的其他文件夹中,我想在其他文件夹中缓存缩略图,如何更改 sorl-thumbnail 缓存文件夹以及如何为图像提供绝对路径缩略图?
最佳答案
看看the class responsible用于命名 sorl-thumbnail 生成的缩略图。
您可以将它子类化并使用您的自定义类作为缩略图后端:
# in your settings.py:
THUMBNAIL_BACKEND = 'path.to.MyThumbnailBackend'
#some module, in one of yours apps:
from sorl.thumbnail.base import ThumbnailBackend
from sorl.thumbnail.conf import settings
from sorl.thumbnail.helpers import tokey, serialize
class MyThumbnailBackend(ThumbnailBackend):
def _get_thumbnail_filename(self, source, geometry_string, options):
"""
Computes the destination filename.
"""
key = tokey(source.key, geometry_string, serialize(options))
# make some subdirs
path = '%s/%s/%s' % (key[:2], key[2:4], key)
return '%s%s.%s' % (settings.THUMBNAIL_PREFIX, path,
EXTENSIONS[options['format']])
前面的代码片段是 _get_thumbnail_filename
的原始代码。您可以调整此代码以在您方便时生成名称。
关于python - 如何更改或覆盖 sorl-thumbnail 缓存路径并添加具有绝对路径的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16564609/