SmileyChris

sorl-thumbnail ImageWithThumbnailsField

Rambling on about Django 29 months ago.

If you're using sorl-thumbnail, you may want to update svn and check out the new field I recently added.

No docs as of this moment, so just look at the source.

What, you're not afraid of source are you?

Ok, if you insist, here's a summary:

To use, just replace your current ImageField with ImageWithThumbnailsField (which you import from sorl.thumbnail.fields).

It takes an extra argument: thumbnail, which is a dictionary containing the 2-length tuple of the size and optionally a list of thumbnail options. For example, if you used this field:

MyModel(models.Model):
    ...
    photo = ImageWithThumbnailsField(
        upload_to='profiles',
        thumbnail={
            'size': (50, 50),
            'options': ('crop',)
        })

Your model instance will have two new functions:

get_photo_thumbnail
Returns a DjangoThumbnail instance for your pleasure (if you use this in a template, it'll return the full URL to the thumbnail)
get_photo_thumbnail_tag
Returns the HTML <img src="..." width="..." height="..." alt="" /> tag. You may find this useful for showing the thumbnail in admin using list_display.

If you want to use multiple thumbnails, you can also provide a extra_thumbnails dictionary:

photo = ImageWithThumbnailsField(
    upload_to='profiles',
    thumbnail={
        'size': (50, 50),
        'options': ('crop',)
    },
    extra_thumbnails={
        'admin': {
            'size': (60, 30)
        }
    })

Which would another two functions on your instance for each extra thumbnail. In this case, get_photo_admin_thumbnail and get_photo_admin_thumbnail_tag.

Leave a comment or see my other ramblings.

Comments (5)

Simon said: (29 months ago)

Thank you so much. This is perfect.

SAn said: (26 months ago)

realy cool, thanks a lot!

Daniel Jewett said: (24 months ago)

Hi Chris,
This is working well in admin list view (using get_image_thumbnail_tag(), but switching to add or change view produces an error with the following:

global name 'oldforms' is not defined

54. return [oldforms.ImageUploadField, oldforms.HiddenField] ...

▼ Local vars
Variable Value
self
<sorl.thumbnail.fields.ImageWithThumbnailsField object at 0x1977c50>

What can I do about that?

Thanks. Oh and beautiful baby!

Daniel Jewett said: (24 months ago)

Well, I added:

from django import oldforms

to 'sorl/thumbnails/fields.py'

Seems to work for now.

Regards,
Dan J.

:) Chris said: (24 months ago)

Thanks for your feedback, Daniel. Stuff like this is best tracked in the issue tracker: http://code.google.com/p/sorl-thumbnail/issues/...

Leave a comment