gpt4 book ai didi

python - 缩略图现在显示在 django 管理 ListView 中

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:37 25 4
gpt4 key购买 nike

我已经创建了一个基本的文件上传应用程序,我希望能够在 django 管理 ListView 中查看图像的缩略图。我已经尝试在这篇博文中实现代码 - 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)

管理员.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)

谢谢!

最佳答案

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

模型.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。

希望这能让任何人免去我必须经历的痛苦!

关于python - 缩略图现在显示在 django 管理 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16541329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com