[英]View thumbnails of uploaded images in django admin
I am trying to add thumbnails to list view in django admin. 我正在尝试将缩略图添加到django admin中的列表视图。
This is my admin.py for my app, i got this far: 这是我的应用程序的admin.py,我到此为止:
from django.contrib import admin
from .models import Image
from django import forms
from django.db import models
class ImageAdmin(admin.ModelAdmin):
list_display = ("label","tag","order",'Edit','image_img')
list_editable= ("order",)
list_filter = ('tag',)
search_fields = ('tag', 'label')
ordering = ['-order']
list_display_links = ('Edit', )
def Edit(self, obj):
return "Edit"
def image_img(self,obj):
if obj.pic:
return '<img src="%s" height="100px"/>' % obj.pic.url
else:
return 'No_image'
image_img.short_description = 'Image'
image_img.allow_tags = True
admin.site.register(Image,ImageAdmin)
My object for picture is called "pic". 我的图片对象称为“图片”。 If I simply add it to list view django returns link with full path.
如果我只是将其添加到列表视图,那么django将返回具有完整路径的链接。 I was looking for template for list view so that I can add |safe filter but I couldn't find it.
我正在寻找列表视图的模板,以便可以添加安全过滤器,但找不到它。 Any other idea how to achieve it?
还有其他想法如何实现吗?
If you're using Django > 2.0, you can mark the routine as safe using the mark_safe()
decorator: 如果您使用的是Django> 2.0,则可以使用
mark_safe()
装饰器将例程标记为安全:
from django.utils.safestring import mark_safe
@mark_safe
def image_img(self,obj):
if obj.pic:
return '<img src="%s" height="100px"/>' % obj.pic.url
else:
return 'No_image'
It can also be used as a function: 它也可以用作功能:
def image_img(self,obj):
if obj.pic:
return mark_safe('<img src="%s" height="100px"/>' % obj.pic.url)
else:
return 'No_image'
In older versions of Django, I believe you can just set the allow_tags
option to true: 在较旧的Django版本中,我相信您可以将
allow_tags
选项设置为true:
image_img.allow_tags = True # Put this after the function definition
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.