B
B
blazer052016-01-15 13:51:17
Django
blazer05, 2016-01-15 13:51:17

Django how to display images in admin?

Hello.
I'm trying to make pictures for the site. I made a model and brought it into a template - everything works. Can't get it to admin.
It displays in the admin panel like in the picture.
ffb374cda3904afdb9b8c594a2b12a08.jpg
Link to the picture, if you click on it, the picture opens, but it does not open in the news, but simply in a separate window - is this how it should be?
And in the Thumb column there should be the picture itself in small form, but for some reason the code outputs from the function there?

def image_img(self):
        if self.image:
            return u'< img src="%s" width="100"/>' % self.image.url
        else:
            return '(none)'
    image_img.short_description = 'Thumb'
    image_img.allow_tags = True

Where is the error, tell me how to display it correctly in the admin panel?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
blazer05, 2016-01-23
@blazer05

I solved the problem myself, the solution is here !

M
marazmiki, 2016-01-15
@marazmiki

In this context, self is not a model object, but the admin panel itself. You need something like this:

def image_img(self, image):
    if image.image:
            return u'< img src="%s" width="100"/>' % image.image.url
        else:
            return '(none)'
    image_img.short_description = 'Thumb'
    image_img.allow_tags = True

V
Vladislav Sklyar, 2016-01-15
@VladSkliar

Can be done with a battery

S
skiesx, 2016-01-18
@skiesx

In the admin class you need to add:

def icon_tag(self, obj):
    if not (obj.pk and obj.icon):
            return ''
    return u'<img src="%s" />' % obj.icon.url
icon_tag.short_description = 'Icon'
icon_tag.allow_tags = True

And add to the ridonly:
readonly_fields = ('icon_tag',)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question