Login

Simple admin list thumbnail view

Author:
robharvey
Posted:
April 8, 2007
Language:
Python
Version:
.96
Score:
6 (after 6 ratings)

This is a very simple way to display images within the admin list view. It is not efficient in the sense that the images are being downloaded in original format, however for cases where the images are not regularly accessed it may be a straightforward option. Can also be tied into WYSIWYG editors like TinyMCE by adding an appropriate href link in the return value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
    def thumbnail(self):
        """
        Display thumbnail-size image of ImageField named src
        Assumes images are not very large (i.e. no manipulation of the image is done on backend)
        Requires constant named MAX_THUMB_LENGTH to limit longest axis
        """
        max_img_length = max(self.get_src_width(), self.get_src_height())
        ratio = max_img_length > MAX_THUMB_LENGTH and float(max_img_length) / MAX_THUMB_LENGTH or 1
        thumb_width = self.get_src_width() / ratio
        thumb_height = self.get_src_height() / ratio
        url = '%s%s' % (settings.ADMIN_MEDIA_PREFIX, self.get_src_url())
        return '<img src="%s" width="%s" height="%s"/>' % (url, thumb_width, thumb_height)
    thumbnail.short_description = 'Image thumbnail'
    thumbnail.allow_tags = True

    class Admin:
        list_display = ('src', 'thumbnail',)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 4 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 4 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 11 months, 3 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year ago
  5. Help text hyperlinks by sa2812 1 year, 1 month ago

Comments

robharvey (on April 17, 2007):

Updated the "ratio" field so that it only changes the image size when it is above the MAX_THUMB_LENGTH.

#

danfreak (on March 19, 2009):

Hey robharvey,

cheers for the snippet!

I'm a django-beginner could you please provide a small example about using this snippet?

Dan

#

Please login first before commenting.