[英]Django uploaded file in admin list
I've read several posts here on this problem and have tried many of the approaches - in the end I returned to the Django docs. 我在这里阅读了有关此问题的几篇文章,并尝试了许多方法-最后,我返回了Django文档。
I'm trying to get the uploaded image to appear in the admin list... 我正在尝试将上传的图片显示在管理员列表中...
My models.py (based on info from here: Django Docs ) 我的models.py(基于来自此处的信息: Django Docs )
from django.db import models
from django.contrib import admin
from django.utils.html import format_html
class File(models.Model):
file = models.ImageField()
description = models.CharField(max_length=200)
def __str__(self):
return self.description
def image_tag(self):
return format_html(
'<img src="{}" />', self.file
)
class FileAdmin(admin.ModelAdmin):
list_display = ('image_tag', 'description')
This, sadly, does nothing however... 不幸的是,这无济于事...
I have tried putting arbitrary HTML as well - just in case file url is bad...but nothing changes. 我也尝试放置任意HTML-以防万一文件url错误...但没有任何变化。
Any ideas would be greatly appreciated - quite frustrated with this now. 任何想法都将不胜感激-现在对此颇为沮丧。
You can also try this in place of format_html
, 您也可以尝试使用
format_html
代替format_html
,
Update models.py
as follows: 如下更新
models.py
:
from django.utils.safestring import mark_safe
def image_tag(self):
if self.file:
return mark_safe('<img src="%s" width="200" height="200"/>' % self.file)
else:
return '(No image found)'
image_tag.short_description = 'Thumb'
In your admin.py
add: 在您的
admin.py
添加:
list_display= ('image_tag',)
There is also a package django-imagekit which can be helpful. 还有一个软件包django-imagekit可能会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.