Question

I am trying to display an image instead of its name in my list_display page.

I am storing the image in my model's banner field. I have made a function that should return the image but I am seeing the string <img src= ....> instead of the image.

There is a character varying field is in my model banner.

def get_banner_image(obj):
    img = u''

    imgtag = mark_safe(u'<img src="http://www.asdd.com/%s">' % (obj.banner))
    img +=imgtag

    return mark_safe( '%s' % (img) )

And in admin.py:

list_display =(get_banner_image)
Was it helpful?

Solution

Put this in your models.py

def get_banner_image(self):
    return u'<img src="http://www.asdd.com/%s" />' % (obj.banner)
get_banner_image.allow_tags = True

See the docs for more details (plus an example).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top