1

I want to show image like this Django admin listview Customize Column Name. I've checked Django Admin Show Image from Imagefield, Django - Display ImageField, Django - How can I display a photo saved in ImageField?.

This is my models.py and admin.py.

#models.py
class Movie(models.Model):
    id = models.IntegerField(primary_key=True)
    master_id = models.IntegerField()
    url = models.CharField(max_length=100)
    cover = models.CharField(max_length=100)
    tittle = models.CharField(max_length=50)
    duration = models.CharField(max_length=10)
    click = models.IntegerField()
    platform_time = models.DateTimeField()
    platform_type = models.IntegerField()
    entry_date = models.DateTimeField()
    enable = models.IntegerField()
    def image_tag(self):
        return u'<img src="%s" />' % self.cover
    image_tag.short_description = 'Image'
    image_tag.allow_tags = True
    class Meta:
        managed = False
        db_table = 'movie'

#admin.py
class MovieAdmin(admin.ModelAdmin):
    list_display = ('id', 'master_id', 'url', 'cover', 'tittle', 'duration', 'click', 'platform_time', 'platform_type', 'entry_date', 'enable')
    search_fields = ('id', 'tittle',)
    list_filter = ( 'enable', )
    readonly_fields = ( 'image_tag', )

Now, this is a part of my interface, there is no image_tag field. Further more, If I modify cover field to image_tag, the cover field is still not image visible.

My interface

Community
  • 1
  • 1
linuxie
  • 127
  • 3
  • 14

3 Answers3

1

You have to use an ImageField for cover instead of CharField and also change the image_tag return line:

cover = models.ImageField(upload_to = "images") #use the directory you want here
def image_tag(self):
    return u'<img src="%s" />' % self.cover.url

Also you have to add the image_tag field to list_display. And make sure that django can find the upload_to directory.

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
ger.s.brett
  • 3,267
  • 2
  • 24
  • 30
  • Should I must use uploaded image? The image url I want to show is just a link to other website, does django have any idea to show that? – linuxie Nov 12 '14 at 14:35
  • You would have to download the image from the other website first and then upload it. I do not think it would be a good idea to have a direct link to another website in there. – ger.s.brett Nov 12 '14 at 15:56
0

You have no 'image_tag' column (which contains <img...>) in list_view.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
NiTr0
  • 1
  • 2
0

I wrote valid answer about images in django-admin listview for django 2.0.6.

Here is link on my answer: Django: Display image in admin interface

I hope it will help someone.

TomRavn
  • 1,134
  • 14
  • 30