2

I'm testing out a few options. I have uploaded a file using django-storages with S3 and it works fine. However, when I try and read the file with the code below I get the following error:

'FieldFile' object has no attribute 'startswith'

Why?

Even default_storage.exists(self.filepath) give the same error.

 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()

Full Error:

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:
4

2 回答 2

2

错误由以下人员引发:

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

它告诉我们的类型self.filepathFieldFile。因此,鉴于它的名称,我希望该对象是一个已经打开的文件。所以你应该尝试:

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

但我可能是错的,并且self.filepath可能是一个包含您要打开的文件的路径的文件......鉴于您的代码示例,很难判断,我只能猜测。

于 2013-06-26T13:25:17.277 回答
0

您正在传递一个FieldFile对象,default_storage.open()但它需要文件名作为字符串。给它一个用于保存文件而不是FieldFile对象的名称。

于 2013-06-26T13:35:53.170 回答