【问题标题】:Django - add image to list display in django adminDjango - 将图像添加到 django admin 中的列表显示
【发布时间】:2017-03-15 23:56:54
【问题描述】:

请帮助我。我放弃。我正在尝试向我的 django 管理员添加其他字段。我想在那里插入图像缩略图。这是我的 admin.py 的一部分:

class SiteAdmin(admin.ModelAdmin):
    list_display = ('is_active', 'name', 'description', 'keywords', 'date')
    fields = ('name', 'url', 'category', 'subcategory', 'category1',
          'subcategory1', 'description',
          'keywords', 'date', 'group', 'email', 'is_active')
    readonly_fields = ('date',)
    list_display_links = ('name',)
    list_filter = ('is_active',)
    actions = [activate_sites, deactivate_sites]

我想在 list_display 中添加“图像”。图像由 thumbalizr 生成。我在models.py中有一个方法:

class Site(models.Model):
    category = models.ForeignKey('Category')
    subcategory = ChainedForeignKey(
        'SubCategory',
        chained_field='category',
        chained_model_field='category',
        show_all=False,
        auto_choose=True,
        blank=True, null=True, default=None)
    name = models.CharField(max_length=70, verbose_name="Tytuł")
    description = models.TextField(verbose_name="Opis")
    keywords = MyTextField(max_length=100, verbose_name="Słowa kluczowe")
    date = models.DateTimeField(default=datetime.now, editable=False)
    url = models.URLField()
    is_active = models.BooleanField(default=False)

    category1 = models.ForeignKey('Category', related_name='category',    blank=True, null=True, default=None)
    subcategory1 = ChainedForeignKey(
        'SubCategory',
        chained_field='category1',
        chained_model_field='category',
        related_name='subcategory',
        show_all=False,
        auto_choose=True, blank=True, null=True)

    group = models.CharField(max_length=10, choices=(('podstawowy', 'podstawowy'),
                                                 ('premium', 'premium')), default='podstawowy',
                         help_text="<div id='group'><ul><li>You can add site to 2 <b>categories</b></li></ul></div>")

    email = models.EmailField(help_text='Podaj adres email')

    def get_absolute_url(self):
        return reverse('site', args=[str(self.category.slug),
                                 str(self.subcategory.slug), str(self.id)])

    def get_thumb(self):
        host = urlparse(self.url).hostname
        if host.startswith('www.'):
            host = host[4:]
        thumb = 'https://api.thumbalizr.com/?url=http://' + host + '&width=125'
        return thumb

它是 get_thumb() 方法。如何将图像带到每个站点并放入我的 django 管理页面?我应该向我的站点模型添加其他字段吗?我不想在我的服务器上存储图像 - 它们直接来自 thumbalizr。

【问题讨论】:

    标签: django django-admin


    【解决方案1】:

    您应该在您的模型管理类中添加一个方法。然后您可以将此方法添加到您的字段列表中。

    class SiteAdmin(admin.ModelAdmin):
        list_display = [..., 'thumb']
        ...
        ...
    
        def thumb(self, obj):
            return "<img src='{}'  width='20' height='20' />".format(obj.get_thumb())
    
        thumb.allow_tags = True
        thumb.__name__ = 'Thumb'
    

    https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

    【讨论】:

      【解决方案2】:

      我在最新的 django 2.0.6 中解决了这个问题。我想在 django-admin 的列表视图中获得图像缩略图和更多细节。

      我在这里发布答案: Django: Display image in admin interface

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-28
        • 1970-01-01
        • 2014-08-05
        • 2020-10-07
        • 2016-04-23
        • 2012-07-05
        • 2013-04-24
        相关资源
        最近更新 更多