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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.