Django:在管理界面显示图像 [英] Django: Display image in admin interface

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

问题描述

我已经定义了一个包含图像链接的模型。有没有办法在模型项目列表中显示图像?我的模型看起来像这样:

 类文章(models.Model):
url = models.CharField(max_length = 200,unique = True)
title = models.CharField(max_length = 500)
img = models.CharField(max_length = 100)#包含映像的路径

def __unicode __自我):
return u%s%title

有没有办法显示图像与标题?

解决方案

您可以使用另一个名称创建一个模型实例方法,允许HTML标签输出并添加此方法作为列表字段。这是一个例子:



首先添加一个新方法返回图像包含的HTML:

  class Article(models.Model):
...
def admin_image(self):
return'< img src =%s/> '%self.img
admin_image.allow_tags = True

然后将此方法添加到列表中:


$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$''''''''''''''''''''''' ,'title','admin_image')


I've defined a model which contains a link an image. Is there a way to display the image in the model items list? My model looks like this:

class Article(models.Model):
    url = models.CharField(max_length = 200, unique = True)
    title = models.CharField(max_length = 500)
    img = models.CharField(max_length = 100) # Contains path to image

    def __unicode__(self):
       return u"%s" %title

Is there a way to display the image together with title?

解决方案

You can create a model instance method with another name, allow HTML tags for its output and add this method as a list field. Here is an example:

First add a new method returning the HTML for the image inclusion:

class Article(models.Model):
    ...
    def admin_image(self):
        return '<img src="%s"/>' % self.img
    admin_image.allow_tags = True

Then add this method to the list:

class ArticleAdmin(admin.ModelAdmin):    
    ...
    list_display = ('url', 'title', 'admin_image')

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

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