[英]How to display uploaded images in "Change List" page in Django Admin?
I'm trying to display uploaded images in "Change List" page in Django Admin:我正在尝试在 Django 管理员的“更改列表”页面中显示上传的图片:
This is my code below:这是我的代码如下:
# "models.py"
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=50)
price = models.DecimalField(decimal_places=2, max_digits=5)
image = models.ImageField()
def __str__(self):
return self.name
# "admin.py"
from django.contrib import admin
from .models import Product
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'price', 'image',)
So, how can I display uploaded images in "Change List" page in Django Admin?那么,如何在Django后台的“更改列表”页面显示上传的图片呢?
You can create a model instance method with another name, allow HTML tags for its output and add this method as a list field.您可以创建一个具有其他名称的模型实例方法,为其输出允许 HTML 标记并将此方法添加为列表字段。 Here is an example:
下面是一个例子:
First add a new method returning the HTML for the image inclusion:首先添加一个新方法,返回包含图像的 HTML:
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')
UPDATE 2020 2020 年更新
Because this answer is getting a lot of traction recently, I decided to make small edit.因为这个答案最近受到了很大的关注,所以我决定做一些小编辑。 According to django doc it is recommended to use function format_html .
根据 django doc 建议使用函数format_html 。 As @chem1st suggested, if you need it only for admin, everything can be made in admin.py only.
正如@chem1st 建议的那样,如果您只需要管理员使用它,那么一切都只能在admin.py 中进行。
My models.py我的模型.py
from django.db import models
class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField()
price = models.DecimalField(decimal_places = 2, max_digits = 20, default = 00.00)
image = models.ImageField(upload_to=change_image_name, null=True, blank=True)
def __str__(self):
return self.title
My admin.py我的管理员.py
from django.contrib import admin
from django.utils.html import format_html
from .models import Product
class ProductAdmin(admin.ModelAdmin):
list_display = ('title', 'description', 'price', 'image_tag')
def image_tag(self,obj):
return format_html('<img src="{0}" style="width: 45px; height:45px;" />'.format(obj.image.url))
admin.site.register(Product, ProductAdmin)
UPDATE django 2.0.6更新 Django 2.0.6
I was solving this problem in latest django 2.0.6 .我在最新的django 2.0.6 中解决了这个问题。 I wanted have an image thumbnail in a list view in the django-admin.
我想在 django-admin 的列表视图中有一个图像缩略图。
Picture below is my default admin listview.下图是我的默认管理员列表视图。
This is my models.py:
这是我的
models.py:
from django.db import models
from django.utils.safestring import mark_safe
# Create your models here.
class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField()
price = models.DecimalField(decimal_places = 2, max_digits = 20, default = 00.00)
image = models.ImageField(upload_to=change_image_name, null=True, blank=True)
def image_tag(self):
if self.image:
return mark_safe('<img src="%s" style="width: 45px; height:45px;" />' % self.image.url)
else:
return 'No Image Found'
image_tag.short_description = 'Image'
def __str__(self):
return self.title
Please notice I had to use mark_safe() on the image string, otherwise you will get escaped html code instead of a thumbnail in the django-admin
请注意,我必须在图像字符串上使用 mark_safe(),否则您将在 django-admin 中得到转义的 html 代码而不是缩略图
Finally, this is my admin.py
最后,这是我的
admin.py
from django.contrib import admin
from .models import Product
# Register your models here.
class ProductAdmin(admin.ModelAdmin):
list_display = ('title', 'description', 'price', 'image_tag')
admin.site.register(Product, ProductAdmin)
Here we have to register the ProductAdmin class too, I didn't know that and it didn't work.
在这里,我们也必须注册ProductAdmin 类,我不知道这一点并且它不起作用。
def image_tag(self, obj):
return u'<img src="%s" />' % obj.image
image_tag.short_description = 'Image'
image_tag.allow_tags = True
and in your admin.py add:并在您的 admin.py 添加:
readonly_fields = ('image_tag',)
you can also add the image directly in admin您也可以直接在管理员中添加图像
class ArticleAdmin(admin.ModelAdmin):
def admin_image(self, obj):
return '<img src="%s"/>' % obj.img
admin_image.allow_tags = True
list_display = ('url', 'title', 'admin_image')
(I'm using Django 1.5) In addition to following the steps in the accepted answer, I ALSO had to mark the image tag as being "safe" (我正在使用 Django 1.5)除了遵循已接受答案中的步骤之外,我还必须将图像标签标记为“安全”
To do this, wrap the return value with the mark_safe method of django.utils.safestring package.为此,请使用 django.utils.safestring 包的 mark_safe 方法包装返回值。
code example: https://stackoverflow.com/a/14075441/1063031代码示例: https : //stackoverflow.com/a/14075441/1063031
django docs: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#filters-and-auto-escaping django 文档: https : //docs.djangoproject.com/en/dev/howto/custom-template-tags/#filters-and-auto-escaping
You can overwrite django admin view for your model.您可以覆盖模型的 django 管理视图。 See http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates for more details.
有关更多详细信息,请参阅http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates 。
In a brief you need to create a template: templates/admin/your_app/article/change_form.html And there add html to display the image.简而言之,您需要创建一个模板:templates/admin/your_app/article/change_form.html 并添加 html 以显示图像。
http://www.djangosnippets.org/snippets/239/ http://www.djangosnippets.org/snippets/239/
there that, plus, if you're using 1.2 you can combine the above with read-only fields.此外,如果您使用的是 1.2,则可以将上述内容与只读字段结合使用。
Per @TomRavn comment, mark_safe works for me.根据@TomRavn 的评论, mark_safe对我有用。
https://docs.djangoproject.com/en/3.1/ref/utils/#module-django.utils.html https://docs.djangoproject.com/en/3.1/ref/utils/#module-django.utils.html
from django.utils.html import format_html class Test(models.Model): title =... photo =... def image(self): return format_html('<img src="%s"/>' % self.photo.url)
@admin.register(models.Test) class testAdmin(admin.ModelAdmin): list_display = ('title','image')
You need to create image_tag
column with image_tag()
then assign it to list_display as shown below:您需要使用
image_tag()
创建image_tag
列,然后将其分配给list_display ,如下所示:
# "admin.py"
from django.contrib import admin
from .models import Product
from django.utils.html import format_html
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin): # Here
list_display = ('name', 'price', 'image', 'image_tag')
def image_tag(self, obj): # Here
return format_html(
f'''<a href="{obj.image.url}" target="_blank">
<img
src="{obj.image.url}" alt="{obj.image}"
width="50" height="50"
style="object-fit: cover;"
/>
</a>''')
Then, you can display uploaded images in "Change List" page in Django Admin as shown below:然后,就可以在Django管理后台的“更改列表”页面显示上传的图片,如下图:
You can also see my answer explaining how to display uploaded images in "Change" page in Django Admin.您还可以在 Django Admin 的“更改”页面中看到我解释如何显示上传图片的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.