简体   繁体   中英

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. 在此输入图像描述

No option for cropping.

Please help me to sort this out.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM