Skip to main content
 首页 » 编程设计

python之属性错误 : 'cStringIO.StringO' object has no attribute 'fileno' when using django-imagekit

2025年05月04日22youxin

我正在使用 django-imagekit 处理上传图片,我遇到了以下错误:

AttributeError at /car/7/ 
 
'cStringIO.StringO' object has no attribute 'fileno' 
 
Request Method:     GET  
Request URL:    http://luxingnan.azurewebsites.net/car/7/ 
Django Version:     1.8 
Exception Type:     AttributeError 
Exception Value:     
 
'cStringIO.StringO' object has no attribute 'fileno' 
 
Exception Location:     D:\home\site\wwwroot\env\Lib\site-packages\pilkit\utils.py in 
__enter__, line 248 
Python Executable:  D:\Python27\python.exe 
Python Version:     2.7.8 
Python Path:     
 
[u'D:\\home\\site\\wwwroot\\env\\Lib\\site-packages',  '.',  'D:\\Windows\\SYSTEM32\\python27.zip',  'D:\\Python27\\DLLs',  'D:\\Python27\\lib',  'D:\\Python27\\lib\\plat-win',  'D:\\Python27\\lib\\lib-tk',  'D:\\Python27',  'D:\\Python27\\lib\\site-packages',  'D:\\home\\site\\wwwroot'] 
 
Server time:    Thu, 16 Apr 2015 12:28:26 +0000 

下面是我的代码:

# models.py 
class Carpic(models.Model): 
    picture = models.ImageField('pic',upload_to='car-pictures') 
    picture_slide = ImageSpecField(source='picture', 
        processors=[ResizeToFill(762, 456)], 
        format='JPEG', 
        options={'quality': 60} 
        ) 
# template.html 
{% for pic in pictures %} 
<li><img src="{{pic.picture_slide.url}}"/></li> 
{% endfor %} 

谁能告诉我我该怎么做?谢谢

请您参考如下方法:

刚刚有机会看到这个(和 your GH Issue )。我会在这里包括我的回应,因为这似乎是明智之举(:

So it looks like this is a quirk of Azure but we can definitely fix it in PILKit.

PILKit has a utility for quieting some of PIL's noise. The way it does this is by temporarily replacing stderr (using its file descriptor). Apparently on Azure, stderr is an instance of StringIO (which doesn't have a file descriptor). We'll just have to add a guard to the utility for that case (just like the one for when dev/null isn't writeable). It's a small change but I'm pretty busy at the moment. A PR would be much appreciated!

因此,换句话说,这不是 FileWrapper 的问题(如评论中所建议的),而是 Azure 的假标准错误和 PILKit 的 quiet 实用程序的组合。