Sorl thumbnail not showing thumb in AdminImageMixin
Asked Answered
U

4

8

I'm having trouble with Django 1.3 using django-grappeli and sorl-thumbnail. I have a project that uses this code from the official sorl-thumbnails docs:

# myapp/admin.py
from django.contrib import admin
from myapp.models import MyModel
from sorl.thumbnail.admin import AdminImageMixin

class MyModelAdmin(AdminImageMixin, admin.ModelAdmin):
    pass

This project works well with the debug server and a nice little thumbnail appears in the change form of the admin.

However, in another project, i'm serving my project through WSGI and I have 3 separate domains:

www.example.com 
media.example.com (that's serving user uploaded files) 
static.example.com (that's serving static files)

However, in this project, the AdminImageMixin works fine except no thumbnail is available in the changeform for a model:

  • It uploads the picture in the correct place
  • It puts the correct text in the database field (uploads/ + picture_name.jpg) (i verified this with phpmyadmin)
  • It doesn't show any thumbnail in the form besides the browse button (like i'm used to)

Here is some sample code:

# models.py
class Category(models.Model):
    name = models.CharField(max_length=200, verbose_name='name', help_text='Name of category')
    description = models.TextField(verbose_name='Description', help_text='You can use Textile')
    icon = ImageField(upload_to='uploads/', blank=True, null=True)

# admin.py
class CategoryAdmin(AdminImageMixin, admin.ModelAdmin):
    pass
admin.site.register(Category, CategoryAdmin)

# settings.py

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'grappelli',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    'django_evolution',
    'django_extensions',
    'sorl.thumbnail',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

Any ideea what I'm doing wrong?

Thank you in advance.

Underworld answered 27/3, 2011 at 15:39 Comment(0)
N
0

Did you remember to put the sorl.thumbnail in your INSTALLED_APPS and sync the database after it. In case you didn't there isn't a table for the key value pairs and it wont work. I suppose you are using the default database as your key value storage, not redis.

Necrolatry answered 26/8, 2011 at 11:12 Comment(0)
A
0

I run into the same problems, turns out the PIL I installed didn't have jpeg support to make the actual thumbnails although it never showed any error. This is how I fixed it:

  • install jpeg support

    sudo apt-get install libjpeg libjpeg-dev
    

On MAC:

brew install jpeg
  • reinstall PIL

    pip install -I PIL
    

After recompiling it should show that the jpeg support is available, refresh your admin page and you should see the thumbnails.

Address answered 9/6, 2013 at 22:5 Comment(0)
C
0

Check with a debugger if the form field is using the correct widget.

I had the same problem when i was inheriting from 3 different admin classes:

  • django-mptt : MPTTModelAdmin
  • django-modeltranslation : TranslationAdmin
  • sorl-thumbnail : AdminImageMixin

I'm pretty sure that (in my case) django-modeltranslation is overriding the behaviour of sorl-thumbnail changing the "widget" attribute of the ImageField field from AdminImageWidget.

I forced the widget to AdminImageWidget on the get_form function like this:

def get_form(self, request, obj=None, **kwargs):
    kwargs = self._do_get_form_or_formset(request, obj, **kwargs)
    form = super(CategoryAdmin, self).get_form(request, obj, **kwargs)

    form.base_fields['background'].widget = AdminImageWidget()

    return form

At this point using the mixing AdminImageMixin is optional.

Cruller answered 30/1, 2014 at 22:46 Comment(0)
H
0

./manage.py makemigrations thumbnail ./manage.py migrate thumbnail

If you get permission issue look at https://stackoverflow.com/a/41541666

Hohenstaufen answered 20/1, 2017 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.