Django - 将图像添加到django admin中的列表显示

时间:2017-03-15 23:56:55

标签: django django-admin

请帮帮我。我放弃。我想为我的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]

我喜欢将'image'添加到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。

2 个答案:

答案 0 :(得分:3)

您应该在modeladmin类中添加一个方法。然后,您可以将此方法添加到字段列表中。

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

答案 1 :(得分:0)

我正在最新的 django 2.0.6 中解决了此问题。我想在django-admin的listview中实现图像缩略图和更多详细信息。

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

相关问题