Michał
Michał

Reputation: 655

Adding Image into Django Admin ImageField and displaying it in html

Could anyone give a simple working example how to display in html file images added through ImageField in Django Admin? There are many solutions to specific problems but I could not found one simple tutorial how to do it properly.

Upvotes: 0

Views: 1432

Answers (1)

raphv
raphv

Reputation: 1183

How about adding an extra field in the admin class?

In admins.py:

from myapp.models import MyModel
from django.contrib.admin import register, ModelAdmin

@register MyModel
class MyAdmin(ModelAdmin):

    readonly_fields = ('image_preview',)

    def image_preview(self, obj):
        image_field = getattr(obj, 'image', '')
        return format_html(u'<img src="{}" />', image_field.url)

Upvotes: 1

Related Questions