gpt4 book ai didi

python - FieldFile 对象没有属性 'startswith'

转载 作者:太空宇宙 更新时间:2023-11-03 18:55:43 27 4
gpt4 key购买 nike

我正在测试一些选项。我已经使用 django-storages 和 S3 上传了一个文件,它工作正常。但是,当我尝试使用下面的代码读取文件时,出现以下错误:

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

最佳答案

错误是由以下原因引发的:

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

这表明 self.filepath 的类型是 FieldFile。因此,鉴于其名称,我希望该对象是一个已经打开的文件。所以你应该尝试:

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

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

关于python - FieldFile 对象没有属性 'startswith',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17321172/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com