I'm trying to display thumbnails of a model in the admin interface. When I use list_display() to add the image field, it shows the path to the file rather than the image itself. How is it possible to display the image and control its size?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I've solved this by adding:
def image_thumb(self):
return '<img src="/media/%s" width="100" height="100" />' % (self.photo)
image_thumb.allow_tags = True
to the model in models.py
回答2:
There have been some fairly detailed answers to this question in the past, try this link.
Django admin and showing thumbnail images
回答3:
By the way, for all noobies like me: It works also in StackedInline and TabularInline, but if you use this solution, you should add in admin.py:
fields = (..., 'image_thumb', ...) # as you have expected
readonly_fields = ['image_thumb'] # without this there will be traceback