Login

Thumbnails in admin using django-thumbnails-works

Author:
dperetti
Posted:
December 9, 2010
Language:
Python
Version:
1.2
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.