我创建了一个基本的文件上传应用程序,我希望能够在 django 管理列表视图中查看图像的缩略图。我已经尝试在这篇博客文章中实现代码 - http://www.acedevs.com/blog/2011/07/11/django-admin-list-view-thumbnails/)。
添加代码后,我有一个名为“幻灯片缩略图”的额外字段,但是当我上传图像时,它只是“无”,因此它将图像转换为缩略图并显示它。
我没有任何错误,所以不确定我到底哪里出错了..
这是我的代码,希望有人能有所启发。
型号:
从 django.db 导入模型 从 sorl.thumbnail.main 导入 DjangoThumbnail 导入操作系统
class File(models.Model):
CATEGORY_CHOICES = (
('Image', 'Image'),
('Document', 'Document')
)
title = models.CharField(max_length=400, help_text="Enter the title of the file, this will appear on the listings page")
file_type = models.CharField(choices=CATEGORY_CHOICES, help_text="Optional, but will help with filtering on listings page.", max_length=200, blank=True, null=True, default=None)
image_upload = models.ImageField(upload_to="images/filesApp", height_field="image_height", width_field="image_width", blank=True, null=True)
file_upload = models.FileField(upload_to="pdf/filesApp", blank=True, null=True)
image_height = models.PositiveIntegerField(null=True, blank=True, editable=False)
image_width = models.PositiveIntegerField(null=True, blank=True, editable=False)
def slide_thumbnail(self, width=300, height=200):
if self.image:
thumb = DjangoThumbnail(self.image, (width, height))
return '{img src="%s" /}' % thumb.absolute_url
return '{img src="/media/img/admin/icon-no.gif" alt="False"}'
slide_thumbnail.allow_tags = True
def __unicode__(self):
return u'Slide: %s - %sx%s' % (self.title, self.image_height, self.image_width)
管理员
from django.contrib import admin
from models import *
def delete_selected(modeladmin, request, queryset):
for element in queryset:
element.delete()
delete_selected.short_description = "Delete selected elements"
class FileAdmin(admin.ModelAdmin):
model = File
actions = [delete_selected]
list_display = ('title', 'file_type', 'slide_thumbnail')
admin.site.register(File, FileAdmin)
谢谢!