2

我创建了一个基本的文件上传应用程序,我希望能够在 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)

谢谢!

4

2 回答 2

1

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

模型.py

从 django.db 导入模型 从 sorl.thumbnail.main 导入 DjangoThumbnail 导入操作系统

类文件(模型。模型):

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)

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

希望这可以节省任何人我不得不经历的痛苦!

于 2013-05-15T11:14:43.733 回答
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])
于 2013-05-14T11:16:03.507 回答