Answer the question
In order to leave comments, you need to log in
Battery for image view in Django admin panel?
Good afternoon!
I use this construction
admin.py
class BlogPromoHeaderAdmin(admin.ModelAdmin):
list_display = ("id", "promoheader", "get_image")
def get_image(self, obj):
return mark_safe(f'<img src={obj.promoheader.url} width="100"')
get_image.short_description = "Изображение"
Answer the question
In order to leave comments, you need to log in
The error is due to the fact that you are accessing a non-existent field after deletion.
The simplest solution is to move the get_image function to the model and implement it like this
def get_image(self):
try:
return mark_safe(f'<img src={self.promoheader.url} width="100"')
except:
return ""
get_image.short_description = "Изображение"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question