Categories
Python Answers

How to show Image from Imagefield with Python Django Admin?

Spread the love

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',)

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *