python - Django - AttributeError _committed

标签 python django python-imaging-library

我有一个保存图像的表单,一切正常,但我希望能够裁剪图像。但是,当我使用 Pillow 执行此操作时,出现了一个奇怪的错误,该错误并没有真正让我继续下去。

Attribute error at /userprofile/addGame/

_committed

进一步查看追溯,突出显示了以下部分:

我不太确定这个错误是什么意思。我认为这与 form.save(committed=False) 有关,并且在那之后无法编辑文件,但我并不肯定。我可以在该行之后使用 form.user = request.user 编辑用户,所以我认为我试图更改上传的图像是问题的一部分。

最佳答案

这是一个旧帖子,但我最近遇到了类似的问题。 问题中的信息有些不完整,但我有同样的错误。

假设错误是

文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/Image.py”,第 627 行,在 __getattr__ 中 引发 AttributeError(名称)

属性错误:_committed

所以根本问题在于使用 PIL (Pillow)。函数:

image = Image.open(self.cleaned_data['image'])

We wrongly assume that the object image would continue to have the instance of image throughout the function(clean_image in my case).

所以在我的代码结构中,

def clean_image(self):
    if self.cleaned_data['image']:
        try:
            image = Image.open(self.cleaned_data['image'])
            image.verify()
        except IOError:
            raise forms.ValidationError(_('The file is not an image'))
    return image

执行这段代码后会抛出上面提到的错误。这是因为

After several of the other PIL(pillow) functions we use, we need to open the file again. Check this out in pillow documentation

如上面的代码(最后一行),

返回图片

实际上并没有返回任何东西。

修复它

只需添加 ,

image = self.cleaned_data['image'] 

return image 行之前。 代码看起来像,

    except IOError:
        raise forms.ValidationError(_('The file is not an image'))
    image = self.cleaned_data['image']    
return image

关于python - Django - AttributeError _committed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35051929/

相关文章:

django - python社交身份验证重复条目

python - Django Admin 报错admin.E008 The value of fieldsets must be a list or tuple

python - 如何将不是黑色的每个像素转换为某种颜色?

python - 如何在Python中从剪贴板复制图像?

python - 将 PIL 图像保存到 4D Numpy 数组时显示的静态图像

python - 找到带有孔的多个对象的轮廓

python - 如何在 Django 中订购反向外键?

Django 服务器无法与 AzureLogHandler (opencensus) 一起使用

python - 从两个列表的组合中替换 Pandas Column 中的多个值

python - 如何访问类范围内的 "self"?