django-image-cropping没有在管理员中显示裁剪工具

时间:2017-02-15 17:00:59

标签: python django

我试图让django-image-cropping工作。我做了以下步骤:

  1. pip install django-image-cropping
  2. settings.py

    INSTALLED_APPS = [
     [..]
    'easy_thumbnails',
    'image_cropping',
    ]
    [..]
    THUMBNAIL_DEBUG = True
    from easy_thumbnails.conf import Settings as thumbnail_settings
    THUMBNAIL_PROCESSORS = (
        'image_cropping.thumbnail_processors.crop_corners',
    ) + thumbnail_settings.THUMBNAIL_PROCESSORS
    
  3. models.py

    class AboutImg(models.Model):
        image = models.ImageField(blank=True, null=True,
                                  upload_to='uploaded_images')
        cropping = ImageRatioField('image', '500x480')
    
  4. admin.py

    class MyModelAdmin(ImageCroppingMixin, admin.ModelAdmin):
        pass
    
    admin.site.register(AboutImg, MyModelAdmin)
    
  5. views.py

    def about(request):
        aboutimg = AboutImg.objects.order_by('pub_date').first()
        context = {'aboutimg': aboutimg}
        return render(request, 'about.html', context)`
    
  6. about.html

    {% load cropping %}
    
    <img src="{% cropped_thumbnail aboutimg "cropping" %}"   
         class="img-responsive wooimg" alt="{{aboutimg.title}}"
         title="{{aboutimg.title}}" />
    
  7. 当我在管理员中创建新的对象AboutImg时,cropping字段看起来像这样: cropping fails!

    当我在管理面板中保存AboutImg的实例时,我还注意到在我的upload_images/文件夹中,还会从基本图片05_2.jpg创建另外两张图片:

    • 05_2_l3l0SL7.jpg
    • 05_2_l3l0SL7.jpg.1425x500_q85_detail_upscale.jpg

    如果我打开刚刚在管理员中创建的实例,现在基本图片不再是05_2.jpg而是05_2_l3l0SL7.jpg

    enter image description here

    我已经检查过有关此主题的所有stackoverflow问题,但没有一个答案可以解决。

    感谢您的帮助。

    EDIT1:我的环境

     `pip freeze >  
      Django==1.8.3
      Pillow==4.0.0
      appdirs==1.4.0
      django-appconf==1.0.1
      django-image-cropping==1.0.4
      django-taggit==0.22.0
      easy-thumbnails==2.3
      olefile==0.44
      packaging==16.8
      pyparsing==2.1.10
      six==1.10.0
      wsgiref==0.1.2'
    

    / admin的JS控制台说

    jsconsole

2 个答案:

答案 0 :(得分:0)

在settings.py中有正确的路径吗?

MEDIA_ROOT = '/your/local/path//media/'

你在urls.py:

if settings.DEBUG:  
        urlpatterns += patterns('',  
                                #REMOVE IT in production phase  
                                (r'^media/(?P<path>.*)$', 'django.views.static.serve',  
                                {'document_root': settings.MEDIA_ROOT})
          )

答案 1 :(得分:0)

您确定要正确提供媒体文件吗?因为它在django开发模式下默认不活动.. 试试这个https://docs.djangoproject.com/en/1.11/howto/static-files/#serving-files-uploaded-by-a-user-during-development