ha22109
ha22109

Reputation: 8316

Rendering an image instead of its name in a Django page

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)

Upvotes: 0

Views: 417

Answers (1)

Dominic Rodger
Dominic Rodger

Reputation: 99771

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).

Upvotes: 3

Related Questions