M
M
maxclax2014-12-24 01:26:15
Django
maxclax, 2014-12-24 01:26:15

How to display ImageField in Django admin?

I have a field:
image = models.ImageField(upload_to="icon")
with the settings
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/' I
add an image through the admin panel, save it in media/icon. Next, I try to draw a conclusion through img it is not there. I make a direct transition 127.0.0.1:8000/media/icon/i.png it is not there.
Added to distribution:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "media"),
    os.path.join(BASE_DIR, "static"),
)

Why? Maybe some settings are needed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav Grigoriev, 2014-12-24
@maxclax

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