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?
Asked
Active
Viewed 1.0k times
3 Answers
18
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

babbaggeii
- 7,577
- 20
- 64
- 118
-
1Django 1.10 seems to prefer this: `return '
'.format(self.photo.url)` Also, by not specifying height, the photo's original aspect ratio is preserved. – Dylan Dec 12 '17 at 19:46
5
There have been some fairly detailed answers to this question in the past, try this link.
4
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

McGrog
- 41
- 2
-
For further explanation of `'readonly_fields'` part of the handy tip above, see the sentence that refers to `callables` (which `image_thumb` is in the example above) in **ModelAdmin.fields** section: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fields – Jheasly Jan 14 '14 at 03:05