缩略图现在显示在django管理列表视图中

时间:2013-05-14 10:54:50

标签: python django django-models django-admin

我创建了一个基本的文件上传应用程序,我希望能够在django管理列表视图中查看图像的缩略图。我已尝试在此博客帖子http://www.acedevs.com/blog/2011/07/11/django-admin-list-view-thumbnails/)上实施代码。

添加完代码后,我已经获得了额外的字段'幻灯片缩略图'但是当我上传一张图片时,它只是sais' none'所以它将图像转换为缩略图并显示它。

我没有任何错误,所以不确定我错在哪里..

这是我的代码,有人可以说清楚。

MODLES:

来自django.db导入模型 来自sorl.thumbnail.main导入DjangoThumbnail import 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)

谢谢!

2 个答案:

答案 0 :(得分:1)

事实证明,我创建缩略图的函数中的变量是错误的。无论如何,如果有人有兴趣在django管理列表视图中使用缩略图,这是我完成的代码。

models.py

来自django.db导入模型 来自sorl.thumbnail.main导入DjangoThumbnail import 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_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)

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)

请注意:您需要安装sorl-thumbnail。

希望这可以拯救任何人我必须经历的痛苦!

答案 1 :(得分:0)

TypeError是因为使用了FileFieldImageField定义为

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])