Reputation: 127
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.
Upvotes: 1
Views: 5132
Reputation: 1244
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.
Upvotes: 0
Reputation: 3308
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.
Upvotes: 1