django admin - how to display thumbnail instead of

2019-07-19 06:07发布

问题:

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