Search code examples
djangoapachepython-2.7django-adminopensuse

Thumbnail not showing in the django admin interface


Hey guys i have an issue with an app am trying to develop in django. I am trying to show thumbnails in the admin interface but instead it shows the url. Here is what i have in my models.py

from sorl.thumbnail.main import DjangoThumbnail
from django.db import models
from django.contrib.auth.models import User


class Slide(models.Model):

    #Image Fields
    image = models.ImageField(
        upload_to="/srv/www/htdocs/photos/gallery/images/",
        height_field="image_height",
        width_field="image_width"
    )
    image_height = models.PositiveIntegerField(null=True, blank=True, editable=False)
    image_width = models.PositiveIntegerField(null=True, blank=True, editable=False)

    title = models.CharField(max_length=128)
    order = models.PositiveIntegerField(default=0)

    def slide_thumbnail(self, width=300, height=200):
        if self.image:
            thumb = DjangoThumbnail(self.image, (width, height))
            return '{img src="%s" /}' % thumb.absolute_url
        return '{img src="/media/img/admin/icon-no.gif" alt="False"}'
    slide_thumbnail.allow_tags = True

    def __unicode__(self):
        return u'Slide: %s - %sx%s' % (self.title, self.image_height, self.image_width)

And here is what i have in my admin.py

from django.contrib import admin
from models import *
from django.contrib import admin

class SlideAdmin(admin.ModelAdmin):
    list_display = ('title', 'order', 'slide_thumbnail',)
admin.site.register(Slide, SlideAdmin)

I am using django 1.4 on suse 12.1 and running apache as my web server and was trying to follow the tutorial here:

http://www.acedevs.com/blog/2011/07/11/django-admin-list-view-thumbnails/

But instead, I get a url in "slide thumbnail" column instead or an image. The url i get looks like this

{img      src="/srv/www/htdocs/photos/gallery/images/702362_10200241914757028_37708631_n_jpg_300x200_q85.jpg" /}

Could someone tell me what am doing wrong because it uploads the images well but it doesnt display them


Solution

  • You should mark the img tag as safe like this:

    from django.utils.safestring import mark_safe
    
    #... your code
    return mark_safe('<img src="%s" />') % thumb.absolute_url
    

    Hope this leads into the right direction.