How to show Image from Imagefield Django Admin

0 votes

While I can show an uploaded image in list_display is it possible to do this on the per model page?

A quick sample model would be:

Class Model1(models.Model):
    image = models.ImageField(upload_to=directory)

The default admin shows the url of the uploaded image but not the image itself.

Thanks!

Aug 3, 2020 in Python by kartik
• 37,520 points
5,589 views

1 answer to this question.

0 votes

Hello @kartik,

In your model class add a method like:

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

and in your admin.py add:

fields = ( 'image_tag', )
readonly_fields = ('image_tag',)

to your ModelAdmin. If you want to restrict the ability to edit the image field, be sure to add it to the exclude attribute.

Note: With Django 1.8 and 'image_tag' only in readonly_fields it did not display. With 'image_tag' only in fields, it gave an error of unknown field. You need it both in fields and in readonly_fields in order to display correctly.

Hope this helps!!

Thank You!!

answered Aug 3, 2020 by Niroj
• 82,840 points

Related Questions In Python

+5 votes
0 answers

How to send sms to users from Django application?

I'm looking for an option to send ...READ MORE

Nov 13, 2018 in Python by Manu
• 170 points
8,117 views
+1 vote
2 answers

How to automate image upload from my computer to site using python-selenium?

Hey, this code worked for me: image_path=os.path.abspath('.\\folder1\\subfolder2\file1.jpg') driver.find_element_by_id("Id of ...READ MORE

answered Aug 19, 2019 in Python by Rishi
6,225 views
0 votes
1 answer

How to override the django admin translation?

Hello @kartik, Just as you can override a ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,840 points
2,542 views
0 votes
1 answer

How to execute a Python script from the Django shell?

Hello @kartik, The << part is wrong, use < instead: $ ./manage.py shell ...READ MORE

answered Jun 22, 2020 in Python by Niroj
• 82,840 points
10,396 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,437 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
1,555 views
0 votes
1 answer

How do I add a link from the Django admin page of one object to the admin page of a related object?

Hello @kartik, Set show_change_link to True (False by default) in your inline ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,840 points
10,589 views
0 votes
1 answer

How to load a custom JS file in Django admin home?

Hello @kartik, You can override templates/admin/index.html and add the JavaScript ...READ MORE

answered May 14, 2020 in Python by Niroj
• 82,840 points
7,935 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP