2013-05-14 101 views
2

我已經創建了一個基本的文件上傳應用程序,我希望能夠查看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) 

謝謝!

回答

1

原來的變量是錯誤的,我的功能這是創建縮略圖。無論如何,如果任何人有興趣在django管理列表視圖中使用縮略圖,這裏是我的完成代碼。

models.py

從django.db進口車型 從sorl.thumbnail.main進口DjangoThumbnail 進口OS

類文件(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) 

管理。 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) 

Plea注意:您需要安裝sorl-thumbnail。

希望這樣可以節省的人的痛苦我不得不去通過!

0

TypeError是因爲使用FileField的。 ImageField定義爲

class ImageField(upload_to=None[, height_field=None, width_field=None, max_length=100, **options]) 

FileField被定義爲

class FileField(upload_to=None[, max_length=100, **options]) 
+0

是否有可能爲我做什麼,我想的FileField? – JDavies 2013-05-14 11:18:59

+0

我已經刪除了height_field,width_field,因爲FileField不支持它們,就像你提到的那樣。現在我得到這個錯誤 - 異常值:\t FileAdmin.list_display [1],「FILE_TYPE,slide_thumbnail」不是一個可調用或「FileAdmin」的屬性或模型中的「文件」中找到。 – JDavies 2013-05-14 11:29:33

+0

直接從FileField生成預覽將不起作用。但是,您可以單獨生成上傳文件(用於pdf)的預覽圖像,並將其存儲在ImageField中,然後在顯示縮略圖時使用該ImageField – sandy 2013-05-14 11:29:37