我已經創建了一個基本的文件上傳應用程序,我希望能夠查看Django管理列表視圖中的圖像的縮略圖。我已經嘗試在此博客文章上實現代碼 - http://www.acedevs.com/blog/2011/07/11/django-admin-list-view-thumbnails/)。縮略圖現在顯示在Django管理列表視圖
添加代碼後,我有所謂的「幻燈片縮略圖」額外的領域,但是當我上傳的圖像,以便它的形象轉換然後縮略圖,顯示它只是最高審計機關「無」。
我沒有任何錯誤,所以不知道究竟我要去哪裏錯了..
這裏是我的代碼和hopfully有人能提供一些線索。
MODLES:
從django.db進口車型 從sorl.thumbnail.main進口DjangoThumbnail 進口OS
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)
ADMIN.PY
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)
謝謝!
是否有可能爲我做什麼,我想的FileField? – JDavies 2013-05-14 11:18:59
我已經刪除了height_field,width_field,因爲FileField不支持它們,就像你提到的那樣。現在我得到這個錯誤 - 異常值:\t FileAdmin.list_display [1],「FILE_TYPE,slide_thumbnail」不是一個可調用或「FileAdmin」的屬性或模型中的「文件」中找到。 – JDavies 2013-05-14 11:29:33
直接從FileField生成預覽將不起作用。但是,您可以單獨生成上傳文件(用於pdf)的預覽圖像,並將其存儲在ImageField中,然後在顯示縮略圖時使用該ImageField – sandy 2013-05-14 11:29:37