In case this helps anyone, here is one implementation  I have 4
pictures attached to each record but would work the same if only one.

Uses sorl-thumbnails and based on snippet here: 
http://www.djangosnippets.org/snippets/934/
written by baumer1122

In models.py
-----------------
used standard models.ImageField


class Car(models.Model):
    short_desc = models.CharField(max_length=40)
    ...
    pic1 = models.ImageField("Picture 1", blank=True,
upload_to='pics')
    pic2 = models.ImageField("Picture 2", blank=True,
upload_to='pics')
    pic3 = models.ImageField("Picture 3", blank=True,
upload_to='pics')
    pic4 = models.ImageField("Picture 4", blank=True,
upload_to='pics')

In admin.py
---------------
from django.contrib.admin.widgets import AdminFileWidget
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
from django.conf import settings
from PIL import Image
import os

from sorl.thumbnail.main import DjangoThumbnail

try:
    from sorl.thumbnail.main import DjangoThumbnail
    def thumbnail(image_path):
        t = DjangoThumbnail(relative_source=image_path, requested_size=
(200,200))
        return u'<img src="%s" alt="%s" />' % (t.absolute_url,
image_path)
except ImportError:
    def thumbnail(image_path):
        absolute_url = os.path.join(settings.MEDIA_ROOT, image_path)
        return u'<img src="%s" alt="%s" />' % (absolute_url,
image_path)

class AdminImageWidget(AdminFileWidget):
    """
    A FileField Widget that displays an image instead of a file path
    if the current file is an image.
    """
    def render(self, name, value, attrs=None):
        output = []
        file_name = str(value)
        if file_name:
            file_path = '%s%s' % (settings.MEDIA_URL, file_name)
            try:            # is image
                Image.open(os.path.join(settings.MEDIA_ROOT,
file_name))
                output.append('<div class="adminpics"><a
target="_blank" href="%s">%s</a> <a target="_blank" href="%s">%s</
a><br />%s<input name="delete_%s" type="checkbox"></div>%s' % \
                    (file_path, thumbnail(file_name), file_path,
file_name,_('Delete:'), name, _('Change:')))
            except IOError: # not image
                output.append('%s <a target="_blank" href="%s">%s</a>
<br />%s ' % \
                    (_('Currently:'), file_path, file_name, _
('Change:')))

        output.append(super(AdminFileWidget, self).render(name, value,
attrs))
        return mark_safe(u''.join(output))


class CarAdminForm(forms.ModelForm):
    pic1 = forms.ImageField(widget=AdminImageWidget())
    pic2 = forms.ImageField(widget=AdminImageWidget())
    pic3 = forms.ImageField(widget=AdminImageWidget())
    pic4 = forms.ImageField(widget=AdminImageWidget())

    class Meta:
        model = Car



class CarAdmin(admin.ModelAdmin):

    form = CarAdminForm

    def save_model(self, request, obj, form, change):

            #handle delete checkbox for pics
            if request.POST.get('delete_pic1','off')=='on':
                obj.pic1=''

            if request.POST.get('delete_pic2','off')=='on':
                obj.pic2=''

            if request.POST.get('delete_pic3','off')=='on':
                obj.pic3=''

            if request.POST.get('delete_pic4','off')=='on':
                obj.pic4=''

            obj.save()



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to