在Django管理员内联中显示缩略图 [英] Display thumbnail in Django admin inline
本文介绍了在Django管理员内联中显示缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Django的新手,正在尝试更改嵌入式图像在管理面板中的显示方式。我可以在每个当前上方添加预览标签并显示缩略图吗?
i'm new to Django and trying to change how inline images are presented in the admin panel. Can I add a Preview tag above each 'Currently' to and show thumbnail?
推荐答案
经过大量挖掘,我设法做到了。不幸的是,我找不到我使用的来源(过多的标签,太多的标签无法使用。)。因此,我们的想法是创建一个图像小部件,并附加一些html脚本。
I managed to do it after a lot of digging. Unfortunately I couldn't find the source I used (too many tabs, too many didn't work..) . So the idea was to create an Image Widget, that appends some html script.
from django.contrib.admin.widgets import AdminFileWidget
class AdminImageWidget(AdminFileWidget):
def render(self, name, value, attrs=None):
output = []
if value and getattr(value, "url", None):
image_url = value.url
file_name = str(value)
output.append(u' <a href="%s" target="_blank"><img src="%s" alt="%s" width="150" height="150" style="object-fit: cover;"/></a> %s ' % \
(image_url, image_url, file_name, _('')))
output.append(super(AdminFileWidget, self).render(name, value, attrs))
return mark_safe(u''.join(output))
class CollectionImageInline(admin.TabularInline):
formfield_overrides = {models.ImageField: {'widget': AdminImageWidget}}
结果:(无论此问题,我已将管理主题更改为默认主题)
the result : (I changed admin theme to default regardless to this issue)
这篇关于在Django管理员内联中显示缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文