Django image-cropping - 'str' 对象没有属性 '_meta'

Django image-cropping - 'str' object has no attribute '_meta'

我正在使用 django-image-cropping:https://github.com/jonasundderwolf/django-image-cropping

我想要实现的是通过手动简单裁剪。我使用的是 1.7.1 Django 版本。

models.py

class Image(models.Model):
file = models.ImageField('File', upload_to='dependencies/images/photos/')
gallery = models.ForeignKey('Gallery', related_name='images', blank=True, null=True)
cropping = ImageRatioField('file', '370x267', size_warning=True)

views.py

def photos(request):
photos = Image.objects.all()
return render_to_response('photos.html', {'photos':photos}, context_instance=RequestContext(request))

模板

{% load cropping %}
{% for photo in photos %}
    <img src="{% cropped_thumbnail image "cropping" %}" border="0">
{% endfor %}

错误

AttributeError at /photos/
'str' object has no attribute '_meta'
Exception Location:
/venv/lib/python2.7/site-packages/image_cropping/templatetags/cropping.py in cropped_thumbnail, line 16
Python Executable:
/venv/bin/python

我研究了这个错误并发现了这个:

How to add attribute '_meta' to an object?

当我将 1 个元素的 views.py 更改为

'Test.objects.get()' 

不是

'Test.objects.all()'

 test = Test.objects.get(title='test')

测试它是否完美裁剪。

但是如果我想在图像模型中裁剪所有图像并使用 'for loop' 我有这个错误。

为了帮助你更多,这里是第 16 行的部分:

cropping.py

@register.simple_tag(takes_context=True)
def cropped_thumbnail(context, instance, ratiofieldname, **kwargs):
'''
Syntax:
{% cropped_thumbnail instancename "ratiofieldname"
    [scale=0.1|width=100|height=200|max_size="100x200"] [upscale=True] %}
'''

ratiofield = instance._meta.get_field(ratiofieldname)
image = getattr(instance, ratiofield.image_field)  # get imagefield
if ratiofield.image_fk_field:  # image is ForeignKey
    # get the imagefield
    image = getattr(image, ratiofield.image_fk_field)

请帮忙, 谢谢

您将 image 传递给标签,但包含您的图像模型的变量是 photo。更改使用的变量名称。