1. 程式人生 > >Django admin後臺顯示圖片縮圖

Django admin後臺顯示圖片縮圖

直接上程式碼

定義一個upload_img來返回顯示圖片的html
定義顯示圖片說明和allow_tags
mark_safe方法於django.utils.safestring匯入

from django.utils.safestring import mark_safe
from django.contrib import admin

@admin.register(AliOSSFile)
class AliOssUploadAdmin(admin.ModelAdmin):
    def upload_img(self, obj):
        try:
            img = mark_safe('<img src="%s" width="50px" />'
% (obj.file.url,)) except Exception as e: img = '' return img upload_img.short_description = 'Thumb' upload_img.allow_tags = True list_display = ['id', 'ali_id', 'file', 'upload_img', 'created_time'] readonly_fields = ['upload_img']