Django admin and thumbnail display

I am trying to show thumbnails in the Django admin, but I can only see the path to the images, but not the images displayed. I do not know what I am doing wrong.

Server Media Server URL:

from django.conf import settings (r'^public/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}), 

Functional Model:

 def image_img(self): if self.image: return u'<img src="%s" />' % self.image.url_125x125 else: return '(Sin imagen)' image_img.short_description = 'Thumb' image_img.allow_tags = True 

admin.py:

 class ImagesAdmin(admin.ModelAdmin): list_display= ('image_img','product',) 

And the result:

 <img src="http://127.0.0.1:8000/public/product_images/6a00d8341c630a53ef0120a556b3b4970c.125x125.jpg" /> 
+47
python django django-admin
Sep 06 '09 at 7:15
source share
6 answers

This is the source of photologue (see models.py , a little adapted to remove unnecessary things):

 def admin_thumbnail(self): return u'<img src="%s" />' % (self.image.url) admin_thumbnail.short_description = 'Thumbnail' admin_thumbnail.allow_tags = True 

The list_display bit also looks the same, and I know this works. The only thing that seems suspicious to me is your indentation - the two lines starting with image_img at the end of your models.py code should be at the level def image_img(self): for example:

 def image_img(self): if self.image: return u'<img src="%s" />' % self.image.url_125x125 else: return '(Sin imagen)' image_img.short_description = 'Thumb' image_img.allow_tags = True 
+68
Sep 06 '09 at 7:43
source share

Version 1.9 update

Please note that in Django v.1.9

 image_tag.allow_tags = True 

Strike>

stripped and you should use format_html (), format_html_join () or mark_safe () instead

So your model.py should look like this:

 ... def image_img(self): if self.image: return marksafe('<img src="%s" />' % self.image.url_125x125) else: return '(Sin imagen)' image_img.short_description = 'Thumb' 

and in your admin.py add:

 list_display= ('image_img','product',) readonly_fields = ('image_img',) 

and to add it to the "Edit Mode" of the admin panel in admin.py add:

 fields = ( 'image_img', ) 
+10
Jun 22 '16 at 10:23
source share

Add a method to your model ( models.py ):

 def image_tag(self): return u'<img src="%s" />' % <URL to the image> image_tag.short_description = 'Image' image_tag.allow_tags = True 

and in your ModelAdmin ( admin.py ) add:

 readonly_fields = ('image_tag',) 
+7
Jan 30 '14 at 6:24
source share

Adding to @dominic, I wanted to use this in several models, so I created a function that I could call in each model by entering an image to be displayed.

For example, in one application, I have:

 from django.contrib import admin from .models import Frontpage from ..admin import image_file class FrontpageAdmin(admin.ModelAdmin): list_display = ('image_thumb', ...) image_thumb = image_file('obj.image()') admin.site.register(Frontpage, FrontpageAdmin) 

with image function Frontpage, which returns an image.

In another application, I have:

 from django.contrib import admin from .models import Exhibition from ..admin import image_file class ExhibitionAdmin(admin.ModelAdmin): list_display = ('first_image_thumb', ...) first_image_thumb = image_file('obj.related_object.image', short_description='First Image') admin.site.register(Exhibition, ExhibitionAdmin) 

This allows me to specify the image object and short_description , saving the template in another file. Function:

 from sorl.thumbnail import get_thumbnail from django.conf import settings def image_file(image, short_description='Image'): def image_thumb(self, obj): image = eval(image_thumb.image) if image: thumb = get_thumbnail(image.file, settings.ADMIN_THUMBS_SIZE) return u'<img width="{}" height={} src="{}" />'.format(thumb.width, thumb.height, thumb.url) else: return "No Image" image_thumb.__dict__.update({'short_description': short_description, 'allow_tags': True, 'image': image}) return image_thumb 
+6
Jul 13 2018-12-18T00:
source share

With django-imagekit, you can add any image as follows:

 from imagekit.admin import AdminThumbnail @register(Fancy) class FancyAdmin(ModelAdmin): list_display = ['name', 'image_display'] image_display = AdminThumbnail(image_field='image') image_display.short_description = 'Image' # set this to also show the image in the change view readonly_fields = ['image_display'] 
+1
Aug 02 '16 at 10:39 on
source share

Since allow_tags is deprecated , you should use the format_html function. Sample code is as follows:

file models.py :

 class TestAdminModel(models.Model): img = models.URLField() 

admin.py file:

 from django.utils.html import format_html class TestAdmin(admin.ModelAdmin): list_display = ["id", "img", "thumbnail"] def thumbnail(self, obj): return format_html('<img src="{}" style="width: 130px; \ height: 100px"/>'.format(obj.img)) thumbnail.short_description = 'thumbnail' admin.site.register(models.TestAdminModel, TestAdmin) 

The result looks like this: enter image description here

You can see more details from the Django documentation.

+1
Apr 16 '18 at 9:54
source share



All Articles