3

我正在尝试使用sorl-thumbnail v11.12在 Django 管理员中显示缩略图。

models.py已经按照文档使用了 sorl.thumbnail.ImageField:

Note You do not need to use the sorl.thumbnail.ImageField to use sorl.thumbnail. The standard django.db.models.ImageField is fine except that using the sorl.thumbnail.ImageField lets you plugin the nice admin addition explained in the next section.

但是,我不确定如何配置admin.py. 我已经做了一个manage.py syncdb。目前,我有以下内容,但它没有在 Django 管理中显示缩略图。

我的项目/admin.py

from django.contrib.admin import *
from sorl.thumbnail.admin import AdminImageMixin

class ModelAdmin(AdminImageMixin, ModelAdmin):
    pass

class TabularInline(AdminImageMixin, TabularInline):
    pass

class StackedInline(AdminImageMixin, StackedInline):
    pass

我的项目/myapp/admin.py

from MyProject import admin
from myapp.models import Tours

class ToursAdmin(admin.ModelAdmin):
    list_display = ('name', 'image', 'price')
    search_fields = ('name',)

admin.site.register(Tours, ToursAdmin)
4

1 回答 1

2
# models.py
class Tours(models.Model):
    image = models.ImageField(upload_to="path/")

    def thumb(self):
        return u'<img src="%s" />' % (get_thumbnail(c, "50x50", crop='center', quality=95).url)
    thumbs.short_description = 'Photos'
    thumbs.allow_tags = True

# admin.py
class ToursAdmin(admin.ModelAdmin):
    list_display = ('name', 'thumb', 'price')
于 2014-10-29T12:35:39.693 回答