【问题标题】:Django Admin not Show Image from ImagefieldDjango Admin 不显示来自 Imagefield 的图像
【发布时间】:2015-01-08 19:28:03
【问题描述】:

我想显示这样的图像Django admin listview Customize Column Name。 我检查了Django Admin Show Image from ImagefieldDjango - Display ImageFieldDjango - How can I display a photo saved in ImageField?

这是我的 models.py 和 admin.py。

#models.py
class Movie(models.Model):
    id = models.IntegerField(primary_key=True)
    master_id = models.IntegerField()
    url = models.CharField(max_length=100)
    cover = models.CharField(max_length=100)
    tittle = models.CharField(max_length=50)
    duration = models.CharField(max_length=10)
    click = models.IntegerField()
    platform_time = models.DateTimeField()
    platform_type = models.IntegerField()
    entry_date = models.DateTimeField()
    enable = models.IntegerField()
    def image_tag(self):
        return u'<img src="%s" />' % self.cover
    image_tag.short_description = 'Image'
    image_tag.allow_tags = True
    class Meta:
        managed = False
        db_table = 'movie'

#admin.py
class MovieAdmin(admin.ModelAdmin):
    list_display = ('id', 'master_id', 'url', 'cover', 'tittle', 'duration', 'click', 'platform_time', 'platform_type', 'entry_date', 'enable')
    search_fields = ('id', 'tittle',)
    list_filter = ( 'enable', )
    readonly_fields = ( 'image_tag', )

现在,这是我界面的一部分,没有 image_tag 字段。此外,如果我将封面字段修改为 image_tag,封面字段仍然不可见。

【问题讨论】:

    标签: python django image


    【解决方案1】:

    我在 django 2.0.6 的 django-admin listview 中写了关于图像的有效答案。

    这是我的答案的链接: Django: Display image in admin interface

    我希望它会对某人有所帮助。

    【讨论】:

      【解决方案2】:

      你必须使用 ImageField 代替 CharField 并更改 image_tag 返回行:

      cover = models.ImageField(upload_to = "images") #use the directory you want here
      def image_tag(self):
          return u'<img src="%s" />' % self.cover.url
      

      您还必须将 image_tag 字段添加到 list_display。 并确保django可以找到upload_to目录。

      【讨论】:

      • 我应该使用上传的图片吗?我要显示的图片网址只是指向其他网站的链接,django 有什么想法可以显示吗?
      • 您必须先从其他网站下载图像,然后再上传。我认为直接链接到那里的另一个网站不是一个好主意。
      【解决方案3】:

      list_view 中没有'image_tag' 列(其中包含&lt;img...&gt;)。

      【讨论】:

        猜你喜欢
        • 2013-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多