在django admin中显示图像缩略图

ole*_*ner 1 python django django-admin django-grappelli

我正试图在我的管理员中显示图像缩略图,但我明白了

<img src="/media/photos/huyase_18638553_orig_.jpeg" width=60 height=60 />
Run Code Online (Sandbox Code Playgroud)

但不是图像.我的模特:

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')
Run Code Online (Sandbox Code Playgroud)

我的admin.py:

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

admin.site.register(RentPhoto, RentPhotoAdmin)
Run Code Online (Sandbox Code Playgroud)

我还补充道

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

到我的urls.py. 当我去127.0.0.1:8000/media/photos/huyase_18638553_orig_.jpeg我可以看到图像.没关系.可能有什么不对?
PS我django-grapelli用于我的管理站点.它能打破我的形象吗?

els*_*sar 5

您需要将thumb方法返回的html标记为安全.使用mark_safe