Django管理员并显示缩略图 [英] Django admin and showing thumbnail images
问题描述
我试图在Django管理员中显示缩略图,但是我只能看到图像的路径,而不是渲染的图像。我不知道我在做错什么。
服务器媒体网址:
code>从django.conf导入设置
(r'^ public /(?P< path>。*)$','django.views.static.serve',{'document_root':settings。 MEDIA_ROOT}),
功能模型:
def image_img(self):
/ pre>
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
I'm trying to show thumbnail images in Django admin, but I can only see the path to the images, but not the rendered images. I don't know what I'm doing wrong.
Server media URL:
from django.conf import settings (r'^public/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
Function model:
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',)
And the result:
<img src="http://127.0.0.1:8000/public/product_images/6a00d8341c630a53ef0120a556b3b4970c.125x125.jpg" />
解决方案This is in the source for photologue (see
models.py
, slightly adapted to remove irrelevant stuff):def admin_thumbnail(self): return u'<img src="%s" />' % (self.image.url) admin_thumbnail.short_description = 'Thumbnail' admin_thumbnail.allow_tags = True
The
list_display
bit looks identical too, and I know that works. The only thing that looks suspect to me is your indentation - the two lines beginningimage_img
at the end of yourmodels.py
code should be level withdef image_img(self):
, like this: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
这篇关于Django管理员并显示缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!