[Fixed]-Displaying Image Thumbnails instead of Image Path in Django Admin

1👍

You can use this approach, add this method in your models which have images:

def image_tag(self):
    if self.thumbnail:
        return u'<img src="%s" style="width: 150px;" />' % self.thumbnail.url
    else:
        return 'No Image Found'

then call it in admin.py like this:

list_display = ('image_tag', ...)

Regarding to OOP, you can create a parent model and extend other models which have image from the parent, then define this method on parent class

0👍

Create thumbnails separately, while saving the form or uploading original image. So the model only has reference to original image.

In views.py define the below filter. currency.front_image is a models.ImageField.

@register.filter
def get_thumb(img):
    file = img.url[1:].split("/")
    file[-1] = "thumb_" + file[-1]
    fp = os.path.join("/", *file)
    return fp

In the html do as below.

<td><img src="{{ currency.front_image|get_thumb }}" width="80"></td>

Leave a comment