2013-06-26 49 views
1

我測試了幾個選項。我已經用S3上傳了一個使用django-storage的文件,並且工作正常。然而,當我試着和下面的代碼,我得到以下錯誤讀取文件:FieldFile對象沒有屬性'startswith'

'FieldFile' object has no attribute 'startswith'

爲什麼?

即使是default_storage.exists(self.filepath)也會給出相同的錯誤。

csv_file = default_storage.open(self.filepath, 'r') 


new_object = CSVImport(csvfile=csv_file.read(), 
         model=Contact, 
         modelspy=".", 
         mappings="1=first_name,2=mobile,3=email,4=last_name,5=43,6=16") 

new_object.run() 

完全錯誤:

Environment: 


Request Method: POST 
Request URL: http://127.0.0.1:8000/contacts/upload/configurator/95/ 

Django Version: 1.5.1 
Python Version: 2.7.2 
Installed Applications: 
('django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.sites', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'django.contrib.admin', 
'django.contrib.humanize', 
'django.contrib.sitemaps', 
'south', 
'userena', 
'social_auth', 
'djcelery', 
'storages', 
'endless_pagination', 
'django.contrib.flatpages', 
'django_sagepay', 
'guardian', 
'widget_tweaks', 
'badger', 
'tastypie', 
'accounts', 
'contacts', 
'sms', 
'smartpages', 
'django_sagepay', 
'unsubscribe', 
'core', 
'django_nose', 
'debug_toolbar') 
Installed Middleware: 
('dogslow.WatchdogMiddleware', 
'django.middleware.common.CommonMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'async_messages.middleware.AsyncMiddleware', 
'core.middleware.ssl.SSLRedirect', 
'core.middleware.account.RefreshBalance', 
'debug_toolbar.middleware.DebugToolbarMiddleware') 


Traceback: 
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    115.       response = callback(request, *callback_args, **callback_kwargs) 
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 
    25.     return view_func(request, *args, **kwargs) 
File "/Users/user/Documents/workspace/t/contacts/views.py" in upload_configurator 
    201.    process_upload_Temp(upload_id=upload.id, cleaned_data=data) 
File "/Users/user/Documents/workspace/t/contacts/views.py" in process_upload_Temp 
    222.  upload.process(cleaned_data=cleaned_data) 
File "/Users/user/Documents/workspace/t/contacts/models.py" in process 
    169.   csv_file = default_storage.open(self.filepath, 'r') 
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/site-packages/django/core/files/storage.py" in open 
    36.   return self._open(name, mode) 
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/site-packages/storages/backends/s3boto.py" in _open 
    237.   name = self._normalize_name(self._clean_name(name)) 
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/site-packages/storages/backends/s3boto.py" in _clean_name 
    204.   return os.path.normpath(name).replace('\\', '/') 
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/posixpath.py" in normpath 
    318.  initial_slashes = path.startswith('/') 

Exception Type: AttributeError at /contacts/upload/configurator/95/ 
Exception Value: 'FieldFile' object has no attribute 'startswith' 


I have tried: 

csv_file.read() gives same error. 

Full Error details: 

回答

1

錯誤是由拋出:

csv_file = default_storage.open(self.filepath, 'r') 

它告訴的self.filepath類型是FieldFile。所以給它的名字,我希望這個對象是一個已經打開的文件。所以,你應該嘗試:

new_object = CSVImport(csvfile=self.filepath.read(), ...) 

不過,我可能是錯的,self.filepath可能包含路徑,你要打開的文件的文件......這是很難說給你的代碼示例中,我只能猜測。

+1

我試過csvfile = self.filepath.read(),但是我得到一個很長的錯誤,所有的CSV內容都是:'無法打開指定的csv文件,1111yn,0827654543,test @ test.com,1 \ ned, 123,1111,111 \ name等etc etc – GrantU

+0

和csvfile = self.filepath本身給出錯誤:oercing to Unicode:需要字符串或緩衝區,FieldFile找不到 – GrantU

+0

'csvfile = self.filepath.read()'而不是'csvfile = self.filepath'? – zmo

0

您正在將FieldFile對象傳遞給default_storage.open(),但它期望文件名作爲字符串。給它一個用於保存文件的名稱,而不是FieldFile對象。

+1

如何從self.filepath獲取名稱?我也嘗試過self.filepath.name,但給出了奇怪的錯誤:'無法打開指定的csv文件,然後在錯誤中分割出整個文件內容! – GrantU