我将webp_converter包安装在有文档的这里上
{% load webp_converter %}
This is not working which I want to add static_webp
<img src="{% static_webp 'modelImage.url' %}">
This is working fine
<img src="{{ modelImage.url }}">
From Official Doc says
<img src="{% static_webp 'img/hello.jpg' %}">发布于 2020-08-20 11:54:45
我无法使用{% static_webp 'modelImage.url' %}自定义输出到模板。但我能够转换文件时,上传和存储文件立即以所需的格式(webp)。我的解决方案可能对那些正在开发新项目的人很有用,因为我的方法不假定以前在模型中保存了文件。所以让我们按顺序开始吧。
rename_file),我重写了图像存储位置的路径。函数(rename_file)将文件的扩展重命名为.webp,从而创建正确的obj.url。必须立即这样做。因为obj.url具有只读属性。
从django.db导入模型image_path = os.path.abspath(settings.MEDIA_ROOT +‘/照片/asics/’+ date.today().strftime('%Y/%m/%d/')) rename_file(例如,文件名):if filename.find(‘.’.‘) >= 0: dot_index = (len(filename) - filename.rfind('.’.‘),(1)* (-1)文件名= filename0:dot_index文件名=‘{}。{}’.format(文件名,'webp')返回os.path.join(image_path,文件名)类目录(models.Model):models.ImageField= models.ImageField(upload_to=rename_file,blank=True)https://stackoverflow.com/questions/62409683
复制相似问题