Django 预热VersatileImageField时出错

Django 预热VersatileImageField时出错,django,django-models,Django,Django Models,我已经实现了,并且已经达到了可以使用以下标记成功调整模板中图像大小的状态,例如: 因此,我确信我的form.py、view.py和模板能够正常工作。但是,我最近尝试对图像进行“预热”,这样当用户保存图像时,一组大小调整后的图像会保存到我的“媒体”文件夹中。这可以防止服务器在每次加载页面时都必须动态调整大小,并且只需抓取大小正确的图像 我的问题是,当用户在图像上传表单中保存图像时,他们会得到一个错误 设置.py VERSATILEIMAGEFIELD_SETTINGS = { 'cach

我已经实现了,并且已经达到了可以使用以下标记成功调整模板中图像大小的状态,例如:

因此,我确信我的form.py、view.py和模板能够正常工作。但是,我最近尝试对图像进行“预热”,这样当用户保存图像时,一组大小调整后的图像会保存到我的“媒体”文件夹中。这可以防止服务器在每次加载页面时都必须动态调整大小,并且只需抓取大小正确的图像

我的问题是,当用户在图像上传表单中保存图像时,他们会得到一个错误

设置.py

VERSATILEIMAGEFIELD_SETTINGS = {
    'cache_length': 2592000,
    'cache_name': 'versatileimagefield_cache',
    'jpeg_resize_quality': 70,
    'sized_directory_name': '__sized__',
    'filtered_directory_name': '__filtered__',
    'placeholder_directory_name': '__placeholder__',
    'create_images_on_demand': False,
    'image_key_post_processor': None,
    'progressive_jpeg': False
}

VERSATILEIMAGEFIELD_RENDITION_KEY_SETS = {
    'image_gallery': [
        ('image_large', 'thumbnail__400x400'),
        ('image_small', 'thumbnail__900x900')
    ]
}
models.py

from django.db import models
from django.db.models.signals import post_save
from versatileimagefield.fields import VersatileImageField
from django.dispatch import receiver
from versatileimagefield.image_warmer import VersatileImageFieldWarmer

class ImageGallery(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE, default=None)
    image = VersatileImageField('image_gallery', upload_to=upload_location)

    def __str__(self):
        return self.author.username                                          

@receiver(models.signals.post_save, sender=ImageGallery)
def warm_gallery_images(sender, instance, **kwargs):
    gallery_img_warmer = VersatileImageFieldWarmer(
        instance_or_queryset=instance,
        rendition_key_set='image_gallery',
        image_attr='image_small'
    )
    num_created, failed_to_create = gallery_img_warmer.warm()
回溯

Environment:



Traceback:

File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)

File "C:\Users\jason\Desktop\jason\accounts\views.py" in imagegallery
  513.                 instance.save()

File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py" in save
  729.                        force_update=force_update, update_fields=update_fields)

File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py" in save_base
  769.                 update_fields=update_fields, raw=raw, using=using,

File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\dispatch\dispatcher.py" in send
  178.             for receiver in self._live_receivers(sender)

File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\dispatch\dispatcher.py" in <listcomp>
  178.             for receiver in self._live_receivers(sender)

File "C:\Users\jason\Desktop\jason\accounts\models.py" in warm_gallery_images
  177.     num_created, failed_to_create = gallery_img_warmer.warm()

File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\versatileimagefield\image_warmer.py" in warm
  143.                     reduce(getattr, self.image_attr.split("."), instance)

Exception Type: AttributeError at /accounts/profile/websitesetup5/upload/
Exception Value: 'ImageGallery' object has no attribute 'image_small'
环境:
回溯:
文件“C:\Users\jason\AppData\Local\Programs\Python\36\lib\site packages\django\core\handlers\exception.py”位于内部
35响应=获取响应(请求)
文件“C:\Users\jason\AppData\Local\Programs\Python\36\lib\site packages\django\core\handlers\base.py”位于\u get\u响应中
128response=self.process\u异常\u由\u中间件(e,请求)
文件“C:\Users\jason\AppData\Local\Programs\Python\36\lib\site packages\django\core\handlers\base.py”位于\u get\u响应中
126响应=包装的回调(请求,*回调参数,**回调参数)
文件“C:\Users\jason\AppData\Local\Programs\Python\36\lib\site packages\django\contrib\auth\decorators.py”位于\u包装\u视图中
21返回视图功能(请求,*args,**kwargs)
imagegallery中的文件“C:\Users\jason\Desktop\jason\accounts\views.py”
513save()实例
保存中的文件“C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\models\base.py”
729强制更新=强制更新,更新字段=更新字段)
save\u base中的文件“C:\Users\jason\AppData\Local\Programs\Python\36\lib\site packages\django\db\models\base.py”
769更新\字段=更新\字段,原始=原始,使用=使用,
send中的文件“C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site packages\django\dispatch\dispatcher.py”
178对于自身中的接收者。\u现场\u接收者(发送者)
文件“C:\Users\jason\AppData\Local\Programs\Python\36\lib\site packages\django\dispatch\dispatcher.py”
178对于自身中的接收者。\u现场\u接收者(发送者)
文件“C:\Users\jason\Desktop\jason\accounts\models.py”在warm\u gallery\u图像中
177num\u已创建,未能创建=gallery\u img\u warm.warm()
文件“C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site packages\versatileimagefield\image\u warter.py”位于warm中
143reduce(getattr,self.image\u attr.split(“.”),实例)
异常类型:AttributeError at/accounts/profile/websitesetup5/upload/
异常值:“ImageGallery”对象没有属性“image\u small”
我觉得我已经按照这一页上的说明进行了操作,但仍然会遇到这个错误

我应该注意,我没有安装django REST框架。我的理解是,这种保存后预热图像的方法不需要


非常感谢

看起来
image\u attr
字段应该指向model字段
image

@receiver(models.signals.post_save, sender=ImageGallery)
def warm_gallery_images(sender, instance, **kwargs):
    gallery_img_warmer = VersatileImageFieldWarmer(
        instance_or_queryset=instance,
        rendition_key_set='image_gallery',
        image_attr='image'
    )
    num_created, failed_to_create = gallery_img_warmer.warm()

我没有使用versatileimagefield,但也许你应该使用
image\u attr='image'
而不是
image\u attr='image\u small'
。哇,我想这可能已经奏效了!!!!