Login

Thumbnails in admin using django-thumbnails-works

Author:
dperetti
Posted:
December 9, 2010
Language:
Python
Version:
1.2
Tags:
image admin thumbnail
Score:
0 (after 0 ratings)

Very straightforward way to display a thumbnail in the admin using django-thumbnails-works .

django-thumbnails-works requires cropresize (which requires and installs PIL).

Add 'thumbnail_works'to INSTALLED_APPS in settings.py and here you go.

Tested in django 1.3 alpha.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
**models.py**

    class Person(models.Model):
        name = models.CharField(max_length=200)
        photo = EnhancedImageField(
            upload_to="photos/%Y/%m/%d",
            process_source = dict(
                size='512x384', sharpen=True, upscale=True, format='JPEG'),
            thumbnails = {
                'avatar': dict(size='80x60'),
                'medium': dict(size='256x192', detail=True),
            }
            )

**admin.py**
  
    from django.contrib import admin
    from django.contrib.admin.widgets import AdminFileWidget
    from django import forms
    from django.utils.safestring import mark_safe

    from mysite import models
    from mysite.models import Person
    
    class AdminImageWidget(AdminFileWidget):

        def render(self, name, value, attrs=None):
            output = []
            output.append('<a target="_blank" href="%s"><img src="%s"/></a><br />' % \
                (value.avatar.url, value.avatar.url))
    
            output.append(super(AdminFileWidget, self).render(name, value, attrs))
            return mark_safe(u''.join(output))
    
    
    
    
    class PersonAdmin(admin.ModelAdmin):
        formfield_overrides = {
            models.EnhancedImageField: {'widget': AdminImageWidget},
    }
    
    admin.site.register(Person, PersonAdmin)

More like this

  1. save image from url to django ImageField by kriwil 3 years, 6 months ago
  2. ISBN model field: displays 10- and 13-digit variants and external links by fish2000 5 years, 1 month ago
  3. RecaptchaForm by oggy 6 years, 11 months ago
  4. Markup Selection in Admin by jonathan 7 years, 9 months ago
  5. slugify with transliteration by jezdez 4 years, 6 months ago

Comments

Please login first before commenting.