Tôi biết đây là một câu trả lời khá muộn, nhưng tôi nghĩ một việc thực hiện đầy đủ hơn sẽ giúp ích cho người khác ...
Nếu bạn không có nó đã có django-filer, có easy_thumbnails pip install easy-thumbnails
.
# -*- coding: utf-8 -*-
from django.contrib import admin
from easy_thumbnails.files import get_thumbnailer
from models import Photo
class PhotoAdmin(admin.ModelAdmin):
list_display = ('_thumbnail', 'title',)
list_display_links = ('_thumbnail', 'title',) # This makes the icon clickable too
readonly_fields = ('_thumbnail',)
fields = ('title', 'photo',)
def _thumbnail(self, obj):
if obj.photo:
thumbnailer = get_thumbnailer(obj.photo)
thumb = thumbnailer.get_thumbnail({
'crop': True,
'size': (50, 50),
# Sharpen it up a little, since its so small...
'detail': True,
# Put other options here...
})
# Note: we get the actual width/height rather than
# hard-coding 50, 50, just to be DRYer
return u'<img src="%s" alt="thumbnail: %s" width="%d" height="%d"/>' % (thumb.url, obj.photo.name, thumb.width, thumb.height)
else:
return "[No Image]"
# Optional, Provide a nicer label in the display
_thumbnail.short_description = 'Thumbnail'
# Required, leaves the markup un-escaped
_thumbnail.allow_tags = True
admin.site.register(Photo, PhotoAdmin)
Cảm ơn bạn đã làm rõ, tôi không thể tìm thấy trong tài liệu. – Andy
@Andy, nếu điều đó phù hợp với bạn, hãy nhấp vào dấu kiểm theo điểm số câu trả lời này phải chấp nhận nó. –
Không chắc chắn nhưng tôi nghĩ rằng cú pháp đúng là với '{}', không phải '% s'. Vì vậy, nó sẽ là: 'format_html (u '', obj.admin_thumbnail.url)'. – Paolo