1

I'm trying to display image thumbnail in my admin but I get

<img src="/media/photos/huyase_18638553_orig_.jpeg" width=60 height=60 />

but not the image. My model:

class RentPhoto(models.Model):
    '''
    Photos for RentItem
    Only one photo could be avatar photo
    '''
    photo = models.ImageField(_('Photo'), upload_to='photos/')
    is_avatar = models.BooleanField(_('Avatar'), default=False)
    rentitem = models.ForeignKey(RentItem, related_name='photo')
    description = models.CharField(_('Description'), max_length=250)

    def thumb(self):
        if self.photo:
            return u'<img src="%s" width=60 height=60 />' % (self.photo.url)
        else:
            return u'No image file found'
    thumb.short_description = _('Thumbnail')

My admin.py:

class RentPhotoAdmin(admin.ModelAdmin):
    fields = ('photo', 'is_avatar', 'rentitem', 'description')
    list_display = ('thumb', 'rentitem', 'is_avatar', 'description')

admin.site.register(RentPhoto, RentPhotoAdmin)

I also added

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

to my urls.py. When I go to 127.0.0.1:8000/media/photos/huyase_18638553_orig_.jpeg I can see image. It's ok. What could be wrong?
P.S. I use django-grapelli for my admin site. Could it break my images?

oleg.foreigner
  • 993
  • 2
  • 13
  • 28
  • Try using [`mark_safe`](https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.safestring.mark_safe) on the custom html you are returning – elssar Jun 15 '14 at 04:16
  • @elssar that worked!!! thanks! add it as an answer and I'll check it as the right one. – oleg.foreigner Jun 15 '14 at 04:46

1 Answers1

5

You need to mark the html returned by the thumb method as safe. Use mark_safe

elssar
  • 5,651
  • 7
  • 46
  • 71