【问题标题】:Show image thumbnail in django admin在 django admin 中显示图像缩略图
【发布时间】:2014-08-05 05:48:06
【问题描述】:

我正在尝试在我的管理员中显示图像缩略图,但我得到了

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

但不是图像。 我的模型:

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')

我的 admin.py:

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

admin.site.register(RentPhoto, RentPhotoAdmin)

我也加了

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

到我的 urls.py。当我转到 127.0.0.1:8000/media/photos/huyase_18638553_orig_.jpeg 时,我可以看到图像。没关系。有什么问题吗?
附言我的管理站点使用django-grapelli。它会破坏我的图像吗?

【问题讨论】:

  • 尝试在您返回的自定义 html 上使用 mark_safe
  • @elssar 成功了!!!谢谢!添加它作为答案,我会检查它是否正确。

标签: python django django-admin django-grappelli


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 2012-07-21
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 2011-05-05
    相关资源
    最近更新 更多