Menu Close

How to show Image from Imagefield with Python Django Admin?

To show Image from Imagefield with Python Django Admin, we can add our own tag.

For instance, w write

def image_tag(self):
    from django.utils.html import escape
    return u'<img src="%s" />' % escape(<URL to the image>)
image_tag.short_description = 'Image'
image_tag.allow_tags = True

to add the image_tag that returns a string with the img element.

Then in admin.py, we reguister the tag with

fields = ( 'image_tag', )
readonly_fields = ('image_tag',)
Posted in Python, Python Answers