我创建了一个基本的文件上传应用,我希望能够在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)
谢谢!
最佳答案
事实证明,该变量在我创建缩略图的函数中是错误的。无论如何,如果有人对在django管理员列表视图中使用缩略图感兴趣,这是我完成的代码。
models.py
从django.db导入模型
从sorl.thumbnail.main导入DjangoThumbnail
导入操作系统
类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_upload:
thumb = DjangoThumbnail(self.image_upload, (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'File: %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)
请注意:您将需要安装sorl-thumbnail。
希望这可以减轻任何人我必须经历的痛苦!