Django管理员不显示Imagefield的图像 [英] Django Admin not Show Image from Imagefield

查看:245
本文介绍了Django管理员不显示Imagefield的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示像这样的图像 Django管理列表自定义列名称
我检查过 Django管理员从Imagefield显示图像,<一个href =https://stackoverflow.com/questions/17846290/django-display-imagefield> Django - 显示ImageField ,

I want to show image like this Django admin listview Customize Column Name. I've checked Django Admin Show Image from Imagefield, Django - Display ImageField, Django - How can I display a photo saved in ImageField?.

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

This is my models.py and 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,则封面字段仍然不可见。

Now, this is a part of my interface, there is no image_tag field. Further more, If I modify cover field to image_tag, the cover field is still not image visible.

推荐答案

您必须使用ImageField进行封面而不是CharField还要更改image_tag返回行:

You have to use an ImageField for cover instead of CharField and also change the image_tag return line:

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目录。

Also you have to add the image_tag field to list_display. And make sure that django can find the upload_to directory.

这篇关于Django管理员不显示Imagefield的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆