开发者

Sorl thumbnail not showing thumb in AdminImageMixin

开发者 https://www.devze.com 2023-02-19 21:45 出处:网络
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:

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.


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.


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.


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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号