我正在尝试在Django管理员中显示缩略图,但我只能看到图像的路径,但不能看到渲染的图像。我不知道我做错了什么。
服务器媒体网址:
from django.conf import settings
(r'^public/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
功能模型:
def image_img(self):
if self.image:
return u'<img src="%s" />' % self.image.url_125x125
else:
return '(Sin imagen)'
image_img.short_description = 'Thumb'
image_img.allow_tags = True
admin.py:
class ImagesAdmin(admin.ModelAdmin):
list_display= ('image_img','product',)
结果:
<img src="http://127.0.0.1:8000/public/product_images/6a00d8341c630a53ef0120a556b3b4970c.125x125.jpg" />
答案 0 :(得分:69)
这是photologue的来源(请参阅models.py
,略微适合删除不相关的内容):
def admin_thumbnail(self):
return u'<img src="%s" />' % (self.image.url)
admin_thumbnail.short_description = 'Thumbnail'
admin_thumbnail.allow_tags = True
list_display
位看起来也一样,我知道这有效。唯一令我怀疑的是你的缩进 - image_img
代码末尾的models.py
开头的两行应与def image_img(self):
保持一致,如下所示:
def image_img(self):
if self.image:
return u'<img src="%s" />' % self.image.url_125x125
else:
return '(Sin imagen)'
image_img.short_description = 'Thumb'
image_img.allow_tags = True
答案 1 :(得分:10)
请注意,在Django v.1.9
image_tag.allow_tags = True
击> <击> 撞击>
is depricated and you should use format_html(), format_html_join(), or mark_safe() instead
所以你的model.py应该是这样的:
...
def image_img(self):
if self.image:
return marksafe('<img src="%s" />' % self.image.url_125x125)
else:
return '(Sin imagen)'
image_img.short_description = 'Thumb'
并在 admin.py 中添加:
list_display= ('image_img','product',)
readonly_fields = ('image_img',)
并将其添加到“编辑”模式&#39;您的 admin.py 中的管理员面板添加:
fields = ( 'image_img', )
答案 2 :(得分:7)
在模型中添加方法(models.py
):
def image_tag(self):
return u'<img src="%s" />' % <URL to the image>
image_tag.short_description = 'Image'
image_tag.allow_tags = True
并在您的ModelAdmin(admin.py
)中添加:
readonly_fields = ('image_tag',)
答案 3 :(得分:6)
添加到@dominic,我想在多个模型中使用它,所以我创建了一个我可以在每个模型中调用的函数,输入要显示的图像。
例如,我在一个应用程序中:
from django.contrib import admin
from .models import Frontpage
from ..admin import image_file
class FrontpageAdmin(admin.ModelAdmin):
list_display = ('image_thumb', ...)
image_thumb = image_file('obj.image()')
admin.site.register(Frontpage, FrontpageAdmin)
使用image
Frontpage函数返回图像。
在我的另一个应用程序中:
from django.contrib import admin
from .models import Exhibition
from ..admin import image_file
class ExhibitionAdmin(admin.ModelAdmin):
list_display = ('first_image_thumb', ...)
first_image_thumb = image_file('obj.related_object.image',
short_description='First Image')
admin.site.register(Exhibition, ExhibitionAdmin)
这允许我指定图像对象和short_description
,同时将样板文件保存在另一个文件中。功能是:
from sorl.thumbnail import get_thumbnail
from django.conf import settings
def image_file(image, short_description='Image'):
def image_thumb(self, obj):
image = eval(image_thumb.image)
if image:
thumb = get_thumbnail(image.file, settings.ADMIN_THUMBS_SIZE)
return u'<img width="{}" height={} src="{}" />'.format(thumb.width, thumb.height, thumb.url)
else:
return "No Image"
image_thumb.__dict__.update({'short_description': short_description,
'allow_tags': True,
'image': image})
return image_thumb
答案 4 :(得分:1)
使用django-imagekit,您可以添加以下任意图片:
from imagekit.admin import AdminThumbnail
@register(Fancy)
class FancyAdmin(ModelAdmin):
list_display = ['name', 'image_display']
image_display = AdminThumbnail(image_field='image')
image_display.short_description = 'Image'
# set this to also show the image in the change view
readonly_fields = ['image_display']
答案 5 :(得分:1)
自allow_tags has been deprecated以来,您应该使用format_html
功能。示例代码如下所示:
models.py
档案:
class TestAdminModel(models.Model):
img = models.URLField()
admin.py
档案:
from django.utils.html import format_html
class TestAdmin(admin.ModelAdmin):
list_display = ["id", "img", "thumbnail"]
def thumbnail(self, obj):
return format_html('<img src="{}" style="width: 130px; \
height: 100px"/>'.format(obj.img))
thumbnail.short_description = 'thumbnail'
admin.site.register(models.TestAdminModel, TestAdmin)
中查看更多详情