這是一箇舊帖子,但我最近遇到過類似的問題。 問題中的信息有點不完整,但我有同樣的錯誤。
假設錯誤是
文件 「/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/Image.py」,線路627,在__getattr__ 提高AttributeError的(名字)
AttributeError的:_committed
所以,問題的根源在於使用PIL(枕頭)。功能:
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
正如上文(最後一行)的代碼,
return image
並沒有真正返回任何東西。
要修復它
只需添加,
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