Jahongir Rahmonov
Jahongir Rahmonov

Reputation: 13763

Change list_display in django

In my model, I have a url field:

url = models.URLField(...)  #  This is a url for an image

And in django admin, it is displayed as text. What I want to do is display the image itself(like a thumbnail).

So I want to edit whatever template this list_display uses, and put that url into an

<img src="..."> 

tag.

I cannot find the proper template to edit.

How to accomplish this?

Upvotes: 0

Views: 1056

Answers (2)

bhushya
bhushya

Reputation: 1317

Here is the quick solution without template modification

models.py files

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'

admin.py file

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',)    

Note: Instead of logo column, you can use your own database column. Also you need to do some changes into the def logo_image(self): method

Upvotes: 0

Hedde van der Heide
Hedde van der Heide

Reputation: 22459

There's no need to change any template, e.g.

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

Upvotes: 3

Related Questions