【发布时间】:2015-09-07 21:31:15
【问题描述】:
目前,我正在使用 django-imagekit 在画廊应用程序的大量图像视图中显示缩略图。为此,我使用了“模型方法”,这意味着我正在模型中创建缩略图。
现在考虑到管理员的舒适度(一次上传多张图片),我还想在管理员视图中实现多上传表单。为了让事情稍微轻松一点,我尝试使用我在 GitHub 上找到的一个应用程序 django-admin-multiupload(由于我的声誉低,我无法链接到它,但这是它在 GitHub 上的确切名称)。 当我只使用 django-imagekit 时,一切正常,我得到了漂亮的缩略图,就像预期的那样。当我只使用 django-admin-multiupload 时,一切正常,我可以像预期的那样上传多张图片。
当我同时使用这两个应用程序时,问题就开始了。多重上传仍然可以正常工作,但是当我打开视图并实际实现缩略图时(仅使用两者而不实现缩略图工作正常),应该显示缩略图的地方我会收到以下错误:
/gallery/ 处的 OSError - 解码器 jpeg 不可用
您可以在此处查看完整错误:http://pastebin.com/gtVYEeG7 当仅启动单个应用程序并且它可以工作时,我的困惑就开始了。据我所知,所有 PIL 问题都不存在。 提供更多信息:我正在使用带有以下软件包列表的 virtualENV:
- 点
- django
- PIL
- 皮包
- django-imagekit
- django-amdin-multiupload
为了也提供一些我的实现代码,这里是:
文件:models.py
class Image(models.Model):
"""the model for the images"""
# the foreign key from the event
event = models.ForeignKey('Event', related_name='images',
blank=True, null=True)
# the image itself
# file = models.FileField('File', upload_to='gallery/images/')
file = models.ImageField('File', upload_to='gallery/images/')
image_thumbnail = ImageSpecField(source='file',
processors=[
ResizeToFill(300, 250)
],
format='JPEG',
options={'quality': 40})
# image title to represent it in the admin interface
image_name = models.CharField(max_length=35, default='img')
# publication date of the image
pub_date = models.DateTimeField('date published',
auto_now_add=True)
# for a better representation of the image
def __str__(self):
return self.image_name
文件:admin.py
(这个主要是在 django-admin-multiupload repo 的示例中建议的,可以在这里查看:https://github.com/gkuhn1/django-admin-multiupload/blob/master/example/gallery/admin.py)
from django.contrib import admin
from django.shortcuts import get_object_or_404
from gallery.models import Event, Image
from multiupload.admin import MultiUploadAdmin
# Register your models here.
# allows inline add of single images
class ImageInlineAdmin(admin.TabularInline):
model = Image
# used to define the process_uploaded_file function
# so it will not be duplicated in the Admin-Classes
class GalleryMultiuploadMixing(object):
def process_uploaded_file(self, uploaded, event, request):
image = event.images.create(file=uploaded)
return {
'url': image.file.url,
'thumbnail': image.file.url,
'id': image.id,
'name': image.image_name
}
# admin class for event model
class EventAdmin(GalleryMultiuploadMixing, MultiUploadAdmin):
inlines = [ImageInlineAdmin,]
multiupload_form = True
multiupload_list = False
def delete_file(self, pk, request):
obj = get_object_or_404(Image, pk=pk)
return obj.delete()
admin.site.register(Event, EventAdmin)
# admin class for image model
class ImageAdmin(GalleryMultiuploadMixing, MultiUploadAdmin):
multiupload_form = False
multiupload_list = True
admin.site.register(Image, ImageAdmin)
文件:index.html
<td><img class="img-responsive" src="{{ image.image_thumbnail.url }}" /></td>
如果需要更多信息,请随时提出。 提前感谢您,感谢您的帮助。
编辑:今天我尝试了另一种方法,发现错误仅出现在使用 django-admin-multiupload 上传的图像上,而不是仅显示使用正常上传的图像方法。也许这有助于找到解决方案。
【问题讨论】:
-
另外链接到 GitHub 上的 django-admin-multiupload repo:django-admin-multiupload
标签: python django django-models django-admin