在网格布局中显示图片 djang-admin

display picutre in grid layout djang-admin

首先,我在 Django-admin 中显示缩略图时遇到问题,我尝试了下面的代码,但它似乎不起作用,

class Pictures(models.Model):
    image = models.ImageField(null=True)
    date_added = models.DateField(auto_now_add=True)
    organization = models.ForeignKey(Organisation,on_delete=models.CASCADE)
    

    def __unicode__(self):
        return f"{self}"


    @mark_safe
    def image_img(self):
        if self.image:
            return format_html('<img src="{0}" style="width: 60px; height:65px;" />'.format(self.image.url))
        else:
            return format_html('<img src="" alt={0} style="width: 60px; height:65px;" />'.format("noimagefound"))
            
    image_img.short_description = 'Image'
    image_img.allow_tags = True



class PicturesAdmin(admin.ModelAdmin):

    list_display = ('image', 'date_added','image_img')
    autocomplete_fields = ['organization']

    list_display_links = ('date_added',)

我也想在网格布局中显示这些缩略图,我怎样才能实现任何简单的方法

models.py

from django.utils.safestring import mark_safe
class Pictures(models.Model):
    image = models.ImageField(blank=True,upload_to="pictures/")
    date_added = models.DateField(auto_now_add=True)
    organization = models.ForeignKey(Organisation,on_delete=models.CASCADE)
    

    def __str__(self):
        return f"{self.pk}"


    def image_img(self):
        if self.image:
            return mark_safe('<img src="%s" style="width: 60px; height:65px;" />' % self.image.url )
        else:
            return mark_safe('<img src="" alt="%s" style="width: 60px; height:65px;" />' % "noimagefound")
            
    image_img.short_description = 'Image'

我认为你把 format_html 部分弄错了

    @mark_safe
    def image_img(self):
        if self.image:
            return format_html('<img src="{}" style="width: 60px; height:65px" />', self.image.url)
        else:
            return format_html('<img src="" alt="{}" style="width: 60px; height:65px" />', "noimagefound")