2016-01-28 49 views
3

我有一個保存圖像的窗體,並且一切正常,但我希望能夠裁剪圖像。但是,當我使用Pillow來做到這一點時,我得到了一個奇怪的錯誤,這並不能讓我繼續下去。Django - 屬性錯誤_已投稿

Attribute error at /userprofile/addGame/

_committed

進一步展望回溯,下面的章節中強調:

我真的不知道這是什麼錯誤表示。我認爲這與form.save(committed=False)有關,並且在那之後不能編輯文件,但我不積極。我可以使用form.user = request.user在該行之後編輯用戶,所以我認爲這是事實,即我正在嘗試更改上載的圖像,這是問題的一部分。

回答

1

這是一箇舊帖子,但我最近遇到過類似的問題。 問題中的信息有點不完整,但我有同樣的錯誤。

假設錯誤是

文件 「/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