Thumbnails in admin using django-thumbnails-works

 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. MetaOptions by haagenti 5 years, 10 months ago
  2. ISBN model field: displays 10- and 13-digit variants and external links by fish2000 3 years, 1 month ago
  3. Markup Selection in Admin by jonathan 5 years, 9 months ago
  4. Admin actions as buttons instead of a menu [v2] by itavor 1 year, 5 months ago
  5. Decorating URL includes by cotton 1 year, 9 months ago

Comments

(Forgotten your password?)