python - Django 简单的缩略图使用重复的扩展名得到奇怪的命名
问题描述
我正在使用简单的缩略图,原始文件已正确保存,但缩略图正在以重复的文件扩展名保存:
11.jpg
11.jpg.100x100_q85.jpg
我希望缩略图像这样命名:
11.100x100_q85.jpg
我的模型如下所示:
def image_filename(instance, filename):
folder = 'posts/image'
_, ext = os.path.splitext(filename)
new_name = str(instance.id) + ext
return os.path.join(folder, new_name)
class Post(models.Model):
name = models.CharField(max_length=255, unique=True)
image = ThumbnailerImageField(upload_to=image_filename, null=True, blank=True)
由于我使用 Django Rest Framework,我在这篇文章之后创建了一个序列化程序: Django easy-thumbnails serialize with Django Rest Framework
class ThumbnailSerializer(serializers.ImageField):
def __init__(self, alias, *args, **kwargs):
super().__init__(*args, **kwargs)
self.read_only = True
self.alias = alias
def to_representation(self, value):
if not value:
return None
url = thumbnail_url(value, self.alias)
request = self.context.get('request', None)
if request is not None:
return request.build_absolute_uri(url)
return url
有谁知道如何在缩略图上获得正确的命名?
谢谢!
解决方案
根据图书馆的文档,Link。有 4 个名称可用。
Four namers are included in easy_thumbnails: ``easy_thumbnails.namers.default`` Descriptive filename containing the source and options like ``source.jpg.100x100_q80_crop_upscale.jpg``. ``easy_thumbnails.namers.hashed`` Short hashed filename like ``1xedFtqllFo9.jpg``. ``easy_thumbnails.namers.alias`` Filename based on ``THUMBNAIL_ALIASES`` dictionary key like ``source.jpg.medium_large.jpg``. ``easy_thumbnails.namers.source_hashed`` Filename with source hashed, size, then options hashed like ``1xedFtqllFo9_100x100_QHCa6G1l.jpg``.
由于您没有设置它,它将采用默认选项,即11.jpg.100x100_q85.jpg
.
但是,您可以选择创建自定义命名器。关联
To write a custom namer, always catch all other keyword arguments arguments (with \\*\\*kwargs). You have access to the following arguments: ``thumbnailer``, ``source_filename``, ``thumbnail_extension`` (does *not* include the ``'.'``), ``thumbnail_options``, ``prepared_options``.
也在这里链接
注意:有人还在 Github 上打开了一个问题。关联
推荐阅读
- powershell - 通过子路径在目录中递归查找 n 个匹配文件
- c# - Xamarin - 尽管看似已下载,但未呈现来自 URI 的图像
- java - 找到最长的重复子数组
- java - Java休眠多个一对多和多对一关系
- pinterest - Pinterest pin 功能不起作用
- ruby-on-rails-6 - 配置以允许可选的 belongs_to 关联在 rail 6 中不起作用
- sql - 按日期计算的值差异计数
- c++ - Qt5 - Windows:不一致的 dll 链接错误和 dllimport 静态数据成员的定义不允许
- image - 如何使用 IMPORTXML 导入图像
- linux - egrep 没有在 for 循环中给出结果完整路径