6

This question was already asked ,but that was not solving my problem.i have searched a lot but i am unable to show the images as thumbnail in admin panel.

MY models.py is

thumb_impression=models.ImageField(blank=True, null=True)
cust_pic=models.ImageField(blank=True, null=True ,)

def admin_thumbnail(self):
    return mark_safe(u'<img src="%s" />' % (self.cust_pic))

admin_thumbnail.short_description = 'Thumbnail'
# admin_thumbnail.allow_tags = True

admin_thumbnail = property(admin_thumbnail)

Admin.py is

class AccountAdmin(admin.ModelAdmin):

readonly_fields =  ('balance','terms_and_condition')

fieldsets =  [
    ('Account Details',{
        'fields':['cust_id','cust_first_name','cust_middle_name','cust_last_name','cust_pic']
    }),
    ('Thumbnail',{
        'fields':['cust_pic']
    }),
]

list_display = (
    'cust_first_name',

    'cust_last_name',
    'admin_thumbnail',
)

admin.site.register(Account,AccountAdmin)

urls.py is

urlpatterns=[
url(r'^',include(router.urls)),

url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),

]

I tried previous solution but i am unable to find the correct answer.please help me to find out the solution.please give me a proper example so that i can apply it. Thanks in advance

C P Verma
  • 320
  • 7
  • 23

2 Answers2

3
def admin_thumbnail(self):
    return u'<img src="%s" />' % (self.image.url)
admin_thumbnail.short_description = 'Thumbnail'
admin_thumbnail.allow_tags = True

The list_display bit looks identical too, and I know that works. The only thing that looks suspect to me is your indentation - the two lines beginning image_img at the end of your models.py code should be level with def image_img(self):, like this:

def image_img(self):
    if self.image:
        return u'<img src="%s" />' % self.image.url_125x125
    else:
        return '(Sin imagen)'
image_img.short_description = 'Thumb'
image_img.allow_tags = True
Cp Verma
  • 462
  • 5
  • 19
2

The proper definition as explained by the documentation

class FooAdmin(admin.ModelAdmin):
    list_display = ('bar',)

    def bar(self, obj):  # receives the instance as an argument
        return '<img src="{thumb}" />'.format(
            thumb=obj.image.url,
        )
    bar.allow_tags = True
    bar.short_description = 'sometext'


admin.site.register(Foo, FooAdmin)

Obviously don't add the property to readonly_fields

Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100