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:
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