将ImageFile上传到Django REST后端时遇到以下错误:
substring-before(
substring-after(//app:ApplicationStatusNotificationRequestMessage/
app:Data[contains(., 'applicationId')]/text(),
'applicationId":"'),
'",')
我的观点:
Internal Server Error: /user/addimage/
Traceback (most recent call last):
File "/home/neuron/genie2_env/lib/python3.4/site-packages/django/core/handlers/exception.py", liner
response = get_response(request)
File "/home/neuron/genie2_env/lib/python3.4/site-packages/django/core/handlers/base.py", line 187,e
response = self.process_exception_by_middleware(e, request)
File "/home/neuron/genie2_env/lib/python3.4/site-packages/django/core/handlers/base.py", line 185,e
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/neuron/genie2_env/lib/python3.4/site-packages/django/views/decorators/csrf.py", line 5w
return view_func(*args, **kwargs)
File "/home/neuron/genie2_env/lib/python3.4/site-packages/django/views/generic/base.py", line 68, w
return self.dispatch(request, *args, **kwargs)
File "/home/neuron/genie2_env/lib/python3.4/site-packages/rest_framework/views.py", line 489, in dh
response = self.handle_exception(exc)
File "/home/neuron/genie2_env/lib/python3.4/site-packages/rest_framework/views.py", line 449, in hn
self.raise_uncaught_exception(exc)
File "/home/neuron/genie2_env/lib/python3.4/site-packages/rest_framework/views.py", line 486, in dh
response = handler(request, *args, **kwargs)
File "/home/neuron/genie2/user_profiles/views.py", line 92, in post
profile_stored = file_system_instance.save('profile_' + data['first_name'] + ".jpg", request.FIL)
File "/home/neuron/genie2_env/lib/python3.4/site-packages/django/core/files/storage.py", line 54, e
return self._save(name, content)
File "/home/neuron/genie2_env/lib/python3.4/site-packages/django/core/files/storage.py", line 351,e
for chunk in content.chunks():
File "/home/neuron/genie2_env/lib/python3.4/site-packages/django/core/files/base.py", line 81, in s
data = self.read(chunk_size)
File "/home/neuron/genie2_env/lib/python3.4/site-packages/django/core/files/utils.py", line 16, in>
read = property(lambda self: self.file.read)
AttributeError: 'NoneType' object has no attribute 'read'
[01/Aug/2017 14:14:22] "POST /user/addimage/ HTTP/1.1" 500 18686
我是否完全错了?如果是,请建议更好的解决方案。
答案 0 :(得分:0)
Django REST Framework 3弃用了request.FILES选项。您可以对imagefield使用request.data。
更改
request.FILES.get('profile_pic')
到
request.data.get('profile_pic')
如果您定义了ImageField或FileField,则无需手动上传文件。 Django会为你做这件事
而不是这段代码,
profile_img_instance = ImageFile(request.FILES.get('profile_pic'))
file_system_instance = FileSystemStorage(location=settings.MEDIA_ROOT)
instance.profile_pic = file_system_instance.save('profile_' + data['first_name'] + ".jpg", profile_img_instance)
直接将图像分配到字段
instance.profile_pic = request.data.get('profile_pic')
还要确保您的模型字段看起来像这样,
class Model(models.Model):
profile_pic = models.ImageField(upload_to='profile')