Django的pipe理和显示缩略图图像
我试图在Django的pipe理员中显示缩略图,但我只能看到图像的path,而不是渲染的图像。 我不知道我在做什么错。
服务器媒体url:
from django.conf import settings (r'^public/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
function模式:
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" />
这是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
位看起来也是一样的,我知道这是有效的。 唯一让我怀疑的是你的缩进 – 在models.py
代码的末尾开始image_img
的两行应该是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
更新v。1.9
请注意,在Django v.1.9
image_tag.allow_tags = True
被depricated,你应该使用format_html(),format_html_join()或mark_safe()来代替
所以你的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',)
并将其添加到admin.py添加的pipe理面板的“编辑模式”中:
fields = ( 'image_img', )
在模型中添加一个方法( 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',)
添加到@dominic,我想在多个模型中使用它,所以我创build了一个函数,我可以在每个模型中调用,input要显示的图像。
例如在一个应用程序中,我有:
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返回一个图像的function。
在另一个应用程序,我有:
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
同时将样板保存在另一个文件中。 function是:
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
使用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']