【问题标题】:django - ZeroDivisionError when using thumbnail low-level APIdjango - 使用缩略图低级 API 时出现 ZeroDivisionError
【发布时间】:2015-10-03 14:59:43
【问题描述】:

我通过创建我的自定义表单 (like this) 为以内联表单显示的图像实现了缩略图。

但是,到目前为止,在一种情况下(我注意到),我在尝试编辑项目时收到 ZeroDivisionError,这显然是由 get_thumbnail 调用的 TiffImagePlugin 中的this line 引起的。

这是错误回溯

这是我的代码(注意我也在使用 admin-sortable,但我认为这无关):

from django.contrib.admin.widgets import AdminFileWidget
from django.utils.safestring import mark_safe
from django.forms import ModelForm
from sorl.thumbnail import get_thumbnail
from models import Image

class AdminImageWidget(AdminFileWidget):
    def render(self, name, value, attrs=None):
        output = []
        if value and getattr(value, "url", None):
          t = get_thumbnail(value,'80x80')
          output.append('<img src="{}">'.format(t.url))
        output.append(super(AdminFileWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))

class ImageForm(ModelForm):
    class Meta:
        fields = '__all__'
        model = Image
        widgets = {
          'img': AdminImageWidget,
        }

class ImageInline(SortableStackedInline):
    model = Image 
    extra = 3
    form  = ImageForm

class ProjectAdmin(NonSortableParentAdmin):
    list_display = ('name', 'description')
    inlines = [ImageInline]
    list_filter = ('type',)
    search_fields = ('name', 'description')

请注意,这不会一直发生,它只会在尝试编辑 特定 项目时发生(我假设它也可能发生在其他项目中,但到目前为止还不能查找更多示例)。

有什么想法吗?提前致谢!

【问题讨论】:

    标签: django sorl-thumbnail


    【解决方案1】:

    遇到同样的错误,更新 Pillow,它会有所帮助。

    $ pip install pillow --upgrade
    

    【讨论】:

      猜你喜欢
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 2012-11-18
      • 2018-11-06
      相关资源
      最近更新 更多