Python 更改列表\在django中显示
在我的模型中,我有一个url字段:Python 更改列表\在django中显示,python,django,Python,Django,在我的模型中,我有一个url字段: url = models.URLField(...) # This is a url for an image 在django admin中,它显示为文本。我想做的是显示图像本身(如缩略图) 所以我想编辑这个列表所使用的任何模板,并将该url放入 <img src="..."> 标签 我找不到要编辑的正确模板 如何做到这一点?无需更改任何模板,例如 class FooAdmin(admin.ModelAdmin): list_
url = models.URLField(...) # This is a url for an image
在django admin中,它显示为文本。我想做的是显示图像本身(如缩略图)
所以我想编辑这个列表所使用的任何模板,并将该url放入
<img src="...">
标签
我找不到要编辑的正确模板
如何做到这一点?无需更改任何模板,例如
class FooAdmin(admin.ModelAdmin):
list_display = ('get_url',)
def get_url(self, obj):
return "<img src='{url}' />".format(url=obj.url)
get_url.allow_tags = True
class FooAdmin(admin.ModelAdmin):
列表显示=('get\uURL',)
def get_url(自身、obj):
返回“”格式(url=obj.url)
get_url.allow_tags=True
以下是无需修改模板的快速解决方案
models.py
文件
注意:您可以使用自己的数据库列,而不是徽标列。您还需要对
def logo\u图像(self)进行一些更改:
方法正是我所需要的。ThanxI也尝试了你的答案。它就像一个符咒。谢谢,不是我,我没有足够的名声
class Test(models.Model):
title = models.CharField(max_length=200)
.
.
.
.
logo = ProcessedImageField(upload_to='images/rental/',
processors=[ResizeToFill(291, 167)],
format='JPEG',
options={'quality': 60}, blank=True, null=True)
.
.
.
.
STATUS = (
('active','Active'),
('inactive','Inactive'),
('hidden','Hidden'),
)
status = models.CharField(max_length=10, choices=STATUS, default='active')
def logo_image(self):
return '<img src="/media/%s" height="50px" width="50px"/>' % self.logo
logo_image.allow_tags = True
class Meta:
verbose_name = 'blah'
verbose_name_plural = 'blahsh blah'
class TestAdmin(admin.ModelAdmin):
list_display = ( 'title', 'email', 'address', 'www', 'logo_image', 'status')
list_filter = ('status',)
list_display_links = ('logo_image','title',)#to show link on a image
list_editable = ('status',)