0

I need to show a image preview (small size image) of my order_item in admin.

I'm basically following these other question/answer here:

Django Admin Show Image from Imagefield

However, I cannot get the desired results. I'm getting this instead:

enter image description here

I though it was maybe the URL, but the relative path of that file is the same (except for the static part):

static/media/images/python.png

What could be wrong?

models.py:

class OrderItem(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    product = models.CharField(max_length= 200)
    quantity = models.CharField(max_length= 200)
    size = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name= 'PEN Price')
    image = models.ImageField(upload_to='images', blank=True, null=True)
    comment = models.CharField(max_length=200, blank=True, null=True, default='')
    uploaded_at = models.DateTimeField(auto_now_add=True)



    class Meta:
        db_table = "OrderItem"

    def image_thumbnail(self):
        return u'<img src="%s" />' % (self.image.url)

    image_thumbnail.short_description = 'Image Thumbnail'
    image_thumbnail.allow_tags = True

    def sub_total(self):
        return self.quantity * self.price

admin.py

# Register your models here.

class OrderItemAdmin(admin.TabularInline):
    model = OrderItem
    fieldsets = [
        # ('Customer', {'fields': ['first_name', 'last_name'], }),
        ('Product', {'fields': ['product'],}),
        ('Quantity', {'fields': ['quantity'],}),
        ('Price', {'fields': ['price'], }),
        ('Image', {'fields': ['image'], }),
        ('Image_Thumbnail', {'fields': ['image_thumbnail'], }),
    ]
    readonly_fields = ['product', 'quantity', 'price', 'image', 'image_thumbnail']
    can_delete = False
    max_num = 0
    template = 'admin/order/tabular.html'

### Order Display ###

@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    model = Order
    list_display = ['id', 'first_name', 'last_name', 'email', 'total', 'reason', 'created']
    list_editable = ['reason',]
    list_display_links = ('id', 'email')
    search_fields = ['token', 'shipping_department', 'email']
    readonly_fields = ['id','created']

    fieldsets = [
        ('ORDER INFORMATION', {'fields': ['id','token', 'total', 'created']}),
        # ('BILLING INFORMATION', {'fields': ['billingName', 'billingAddress1', 'billingCity', 'billingPostCode',
        #                                     'billingCountry', 'emailAddress']}),
        ('SHIPPING INFORMATION', {'fields': ['first_name', 'last_name', 'shipping_address', 'shipping_department', 'shipping_province',
                                             'shipping_district', 'shipping_address1', 'shipping_address2']}),
    ]

    inlines = [
        OrderItemAdmin,
    ]

    def has_delete_permission(self, request, obj=None):
        return False

    def has_add_permission(self, request):
        return False
Omar Gonzales
  • 3,806
  • 10
  • 56
  • 120

1 Answers1

3

From Django 1.9, allow_tags has been deprecated, instead you can use mark_safe:

From documentation:

In older versions, you could add an allow_tags attribute to the method to prevent auto-escaping. This attribute is deprecated as it’s safer to use format_html(), format_html_join(), or mark_safe() instead.

So, try like this:

from django.utils.html import mark_safe
...

def image_thumbnail(self):
    return mark_safe('<img src="%s" />' % (self.image.url))
ruddra
  • 50,746
  • 7
  • 78
  • 101