Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-image-cropping not working in admin interface

I'm using django-image-cropping in my django project and I followed the official guidelines but still I'm not getting the desired result. Here the snippet of my project files.

I already added easy_thumbnails and image_cropping to my INSTALLED_APPS.

settings.py

from easy_thumbnails.conf import Settings as thumbnail_settings
THUMBNAIL_PROCESSORS = (
    'image_cropping.thumbnail_processors.crop_corners',
) + thumbnail_settings.THUMBNAIL_PROCESSORS

models.py

from django.db import models
from image_cropping import ImageRatioField    
class UserData(models.Model):
        fullname = models.CharField(max_length=255)
        user = models.CharField(max_length=70, unique=True, blank=False, null=False)
        image = models.ImageField(upload_to=generate_filename,blank=False, null=False)
        cropping = ImageRatioField('image', '180x180')

admin.py

from django.contrib import admin
from image_cropping import ImageCroppingMixin
class UserDataModelAdmin(ImageCroppingMixin, admin.ModelAdmin):
    # filter_horizontal=['image']
    pass

admin.site.register(UserData, UserDataModelAdmin)

As per the official guidelines its enough to see the enhanced selection area in the admin panel, but I'm not getting it. Instead I'm getting this. enter image description here

No option for cropping.

Please help me to sort this out.

like image 958
Rohan Avatar asked Oct 31 '22 04:10

Rohan


2 Answers

Though this isn't covered in the documentation, I found that I needed to add the cropping attribute from my model to my custom UserAdmin fieldsets:

fieldsets = (
    (None, {'fields': ('email', 'password')}),
    ('Personal info', {'fields': ('first_name', 'last_name', 'gender',
        'phone', 'address', 'postal_code', 'city', 'student', 'university',
        'newsletter', 'birthday', 'avatar', 'cropping')}),

Where my image attribute is alternatively names avatar.

like image 190
ryanjdillon Avatar answered Nov 13 '22 16:11

ryanjdillon


Use this tool django-imagekit-cropper, it has nice features and very good documentation. you need to create a form.py file and need to mention this

from django import forms
    from imagekit_cropper.widgets import ImageCropWidget    
    from .models import UserData


    class ImageAdminForm(forms.ModelForm):
        square_150_crop = forms.CharField(widget=ImageCropWidget(properties=Image.square_150_crop_properties, help_text=Image.help['square_150_crop']))

        class Meta:
            model = UserData
like image 24
Rohan Avatar answered Nov 13 '22 16:11

Rohan