Django:在管理界面中显示图像 [英] Django: Display image in admin interface
本文介绍了Django:在管理界面中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我定义了一个包含图像链接的模型.有没有办法在模型项目列表中显示图像?我的模型看起来像这样:
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?
推荐答案
您可以创建一个具有其他名称的模型实例方法,允许为其输出使用 HTML 标记并将此方法添加为列表字段.下面是一个例子:
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:
首先添加一个返回包含图像的 HTML 的新方法:
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屋!
查看全文