我正在尝试使用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管理员中显示缩略图。
MyProject的/ 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
MyProject的/ 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)
答案 0 :(得分:1)
# 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')