这个问题已经被问到了,但这并没有解决我的问题。我已经搜索了很多但我无法在管理面板中将图像显示为缩略图。
我的models.py
thumb_impression=models.ImageField(blank=True, null=True)
cust_pic=models.ImageField(blank=True, null=True ,)
def admin_thumbnail(self):
return mark_safe(u'<img src="%s" />' % (self.cust_pic))
admin_thumbnail.short_description = 'Thumbnail'
# admin_thumbnail.allow_tags = True
admin_thumbnail = property(admin_thumbnail)
Admin.py
类AccountAdmin(admin.ModelAdmin):
readonly_fields = ('balance','terms_and_condition')
fieldsets = [
('Account Details',{
'fields':['cust_id','cust_first_name','cust_middle_name','cust_last_name','cust_pic']
}),
('Thumbnail',{
'fields':['cust_pic']
}),
]
list_display = (
'cust_first_name',
'cust_last_name',
'admin_thumbnail',
)
admin.site.register(帐户,AccountAdmin)
urls.py
urlpatterns=[
url(r'^',include(router.urls)),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
我尝试了以前的解决方案,但我无法找到正确的答案。请帮我找出解决方案。请给我一个正确的例子,以便我可以应用它。 提前致谢
答案 0 :(得分:3)
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
答案 1 :(得分:2)
class FooAdmin(admin.ModelAdmin):
list_display = ('bar',)
def bar(self, obj): # receives the instance as an argument
return '<img src="{thumb}" />'.format(
thumb=obj.image.url,
)
bar.allow_tags = True
bar.short_description = 'sometext'
admin.site.register(Foo, FooAdmin)
显然不要将属性添加到readonly_fields