我试图让django-image-cropping工作。我做了以下步骤:
pip install django-image-cropping
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
models.py
class AboutImg(models.Model):
image = models.ImageField(blank=True, null=True,
upload_to='uploaded_images')
cropping = ImageRatioField('image', '500x480')
admin.py
class MyModelAdmin(ImageCroppingMixin, admin.ModelAdmin):
pass
admin.site.register(AboutImg, MyModelAdmin)
views.py
def about(request):
aboutimg = AboutImg.objects.order_by('pub_date').first()
context = {'aboutimg': aboutimg}
return render(request, 'about.html', context)`
about.html
{% load cropping %}
<img src="{% cropped_thumbnail aboutimg "cropping" %}"
class="img-responsive wooimg" alt="{{aboutimg.title}}"
title="{{aboutimg.title}}" />
当我在管理员中创建新的对象AboutImg
时,cropping
字段看起来像这样:
当我在管理面板中保存AboutImg
的实例时,我还注意到在我的upload_images/
文件夹中,还会从基本图片05_2.jpg
创建另外两张图片:
如果我打开刚刚在管理员中创建的实例,现在基本图片不再是05_2.jpg
而是05_2_l3l0SL7.jpg
。
我已经检查过有关此主题的所有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控制台说
答案 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