Answer the question
In order to leave comments, you need to log in
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"),
)
Answer the question
In order to leave comments, you need to log in
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
readonly_fields = ('icon_tag',)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question