I know how to preview images inside Django Admin with list_display , but since I have about 10 different images in one model, I also need to preview them inline, inside the add / change form , in fact I need to preview them next to the Choose File button (see screenshot):
Thank you for the answer.
No I want to display them after they have been uploaded. By default the path + filename is displayed, which doesn’t bring real value, because you have to click each link to see each image, which is very cumbersome. Do you know a solution?
It looks like you will need to dig through the Django admin HTML templates and figure out what to override. I don’t know of an out of the box solution, sorry.
@saitam1
Six months have already passed so I’m not sure whether you need this now but I can overcome this problem so that I’d like to post.
My solution was the last answer of this page which you introduced.
from django.contrib import admin
from django.utils.safestring import mark_safe
class HOGEModelInline(admin.TabularInline):
model = HOGEModel
readonly_fields = ('image_preview',)
def image_preview(self, obj):
# ex. the name of column is "image"
if obj.image:
return mark_safe('<img src="{0}" width="150" height="150" style="object-fit:contain" />'.format(obj.image.url))
else:
return '(No image)'
image_preview.short_description = 'Preview'
class PIYOAdmin(admin.ModelAdmin):
inlines = [
HOGEModelInline,
]
hi i’m new in django too and i have the same issue but i do not know hot to implement that, that code goes to admin.py and what is PIYOAdmin and HOGEModelInline?