36

I've defined a model which contains a link an image. Is there a way to display the image in the model items list? My model looks like this:

class Article(models.Model):
    url = models.CharField(max_length = 200, unique = True)
    title = models.CharField(max_length = 500)
    img = models.CharField(max_length = 100) # Contains path to image

    def __unicode__(self):
       return u"%s" %title

Is there a way to display the image together with title?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Oleg Tarasenko
  • 8,834
  • 18
  • 68
  • 100

7 Answers7

62

You can create a model instance method with another name, allow HTML tags for its output and add this method as a list field. Here is an example:

First add a new method returning the HTML for the image inclusion:

class Article(models.Model):
    ...
    def admin_image(self):
        return '<img src="%s"/>' % self.img
    admin_image.allow_tags = True

Then add this method to the list:

class ArticleAdmin(admin.ModelAdmin):    
    ...
    list_display = ('url', 'title', 'admin_image')
Michael
  • 8,624
  • 3
  • 36
  • 54
  • Got a problem. Could you take a look pls TypeError at /admin/ 'MediaDefiningClass' object is not iterable Request Method: GET Request URL: http://127.0.0.1:8000/admin/ Exception Type: TypeError Exception Value: 'MediaDefiningClass' object is not iterable – Oleg Tarasenko Mar 15 '10 at 10:00
  • 1
    Actually fixed the previous one, now have this: 'admin_img' is not a callable or an attribute of .... – Oleg Tarasenko Mar 15 '10 at 10:20
  • submited an "edit" to correct the typo. waiting for peer review – jfunez Apr 17 '13 at 05:35
  • Any way to give it a custom label /verbose_name ? Use .short_description – radtek Sep 11 '14 at 13:56
25

UPDATE 2020

Because this answer is getting a lot of traction recently, I decided to make small edit. According to django doc it is recommended to use function format_html. As @chem1st suggested, if you need it only for admin, everything can be made in admin.py only.

My models.py

from django.db import models

class Product(models.Model):
    title = models.CharField(max_length=120)
    description = models.TextField()
    price = models.DecimalField(decimal_places = 2, max_digits = 20, default = 00.00)
    image = models.ImageField(upload_to=change_image_name, null=True, blank=True)

    def __str__(self):
        return self.title

My admin.py

from django.contrib import admin
from django.utils.html import format_html

from .models import Product

class ProductAdmin(admin.ModelAdmin):
    list_display = ('title', 'description', 'price', 'image_tag')

    def image_tag(self,obj):
        return format_html('<img src="{0}" style="width: 45px; height:45px;" />'.format(obj.image.url))

admin.site.register(Product, ProductAdmin)

UPDATE django 2.0.6

I was solving this problem in latest django 2.0.6. I wanted have an image thumbnail in a list view in the django-admin.

Picture below is my default admin listview.

This is my default admin view

This is my models.py:

from django.db import models
from django.utils.safestring import mark_safe

# Create your models here.

class Product(models.Model):
    title = models.CharField(max_length=120)
    description = models.TextField()
    price = models.DecimalField(decimal_places = 2, max_digits = 20, default = 00.00)
    image = models.ImageField(upload_to=change_image_name, null=True, blank=True)

    def image_tag(self):
        if self.image:
            return mark_safe('<img src="%s" style="width: 45px; height:45px;" />' % self.image.url)
        else:
            return 'No Image Found'
    image_tag.short_description = 'Image'
    

    def __str__(self):
        return self.title

Please notice I had to use mark_safe() on the image string, otherwise you will get escaped html code instead of a thumbnail in the django-admin

Finally, this is my admin.py

from django.contrib import admin

from .models import Product
# Register your models here.
class ProductAdmin(admin.ModelAdmin):
    list_display = ('title', 'description', 'price', 'image_tag')

admin.site.register(Product, ProductAdmin)

Here we have to register the ProductAdmin class too, I didn't know that and it didn't work.

This is result: enter image description here

Lord Elrond
  • 10,935
  • 6
  • 28
  • 60
TomRavn
  • 952
  • 8
  • 24
14
def image_tag(self, obj):
    return u'<img src="%s" />' % obj.image

image_tag.short_description = 'Image'
image_tag.allow_tags = True

and in your admin.py add:

   readonly_fields = ('image_tag',)
illagrenan
  • 5,010
  • 2
  • 45
  • 55
Avinash Garg
  • 1,326
  • 13
  • 18
6

you can also add the image directly in admin

class ArticleAdmin(admin.ModelAdmin):

     def admin_image(self, obj):                       
        return '<img src="%s"/>' % obj.img
     admin_image.allow_tags = True

     list_display = ('url', 'title', 'admin_image')
Javed
  • 5,551
  • 3
  • 40
  • 66
1

(I'm using Django 1.5) In addition to following the steps in the accepted answer, I ALSO had to mark the image tag as being "safe"

To do this, wrap the return value with the mark_safe method of django.utils.safestring package.

code example: https://stackoverflow.com/a/14075441/1063031

django docs: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#filters-and-auto-escaping

Community
  • 1
  • 1
pumababi
  • 21
  • 3
0

You can overwrite django admin view for your model. See http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates for more details.

In a brief you need to create a template: templates/admin/your_app/article/change_form.html And there add html to display the image.

Piotr Czapla
  • 24,454
  • 24
  • 93
  • 122
0

Per @TomRavn comment, mark_safe works for me.

https://docs.djangoproject.com/en/3.1/ref/utils/#module-django.utils.html