This code will add a thumbnail image to your Model's Admin list view. The code will also generate the thumb images, so the first view may be a little slow loading.
This assumes you have an ImageField in your Model called image, and the field's upload_to directory has a subdirectory called tiny. You then must add "thumb" to your Model's Admin list_display.
The thumbnail images are also linked to the full size view of the image.
I found this VERY useful... hope someone else does as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<<add to top of file>> import os, Image TINY_SIZE = (80,80) #thumb size (x,y) <<add to Model>> def thumb(self): tinythumb = self.image.replace('\\','/').split('/') tinythumb[-1] = 'tiny/'+tinythumb[-1] tinythumb = '/'.join(tinythumb) if not os.path.exists(MEDIA_ROOT+tinythumb): im = Image.open(MEDIA_ROOT+self.image) im.thumbnail(TINY_SIZE,Image.ANTIALIAS) im.save(MEDIA_ROOT+tinythumb,"JPEG") return """<a href="/media/%s"><img src="/media/%s" alt="tiny thumbnail image" /></a>"""%(self.image,tinythumb) thumb.allow_tags = True
More like this
- Add custom fields to the built-in Group model by jmoppel 1 month, 1 week ago
- Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 4 months, 3 weeks ago
- Python Django CRUD Example Tutorial by tuts_station 5 months ago
- Browser-native date input field by kytta 6 months, 3 weeks ago
- Generate and render HTML Table by LLyaudet 7 months ago