Django Rest Framework:无法序列化或保存上传的图像
Django Rest Framework: Cannot serialize or save uploaded image
无法在 API 视图中序列化或保存上传的图像。
如果保存,则文件已损坏且无法打开。如果序列化,得到:
{"avatar":["No file was submitted."]}
请求内容:
MultiValueDict: {u'name': [<TemporaryUploadedFile: Avatar.jpg (image/jpeg)>]}
设置:
FILE_UPLOAD_HANDLERS = [
'django.core.files.uploadhandler.TemporaryFileUploadHandler',
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
网址:
urlpatterns = [
url(r'^test/$', TestReturn.as_view(), name='test-detail'),
url(r'^users/$', UserProfileAll.as_view(), name='userprofile-detail'),
url(r'^avatar/', UploadAvatar.as_view(), name='images')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
型号:
class AvatarStorage(models.Model):
avatar = models.ImageField(verbose_name="Uploaded image", upload_to='media', null=True)
连载:
class AvatarStorageSerializer(serializers.ModelSerializer):
avatar = serializers.ImageField(use_url=True)
class Meta:
model = AvatarStorage
fields = '__all__'
观看次数:
class UploadAvatar(APIView):
parser_classes = (MultiPartParser,)
def post(self, request, format=None):
serializer = AvatarStorageSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
对于发布,我使用的是 Postman,在那里设置:Body=>Form-Data 并从磁盘中选择一个文件。
如果这很重要,这是 API 移动版
更新设置文件 FILE_UPLOAD_HANDLERS 以点赞
FILE_UPLOAD_HANDLERS=[
'django.contrib.staticfiles.finders.MemoryFileUploadHandler',
'django.contrib.staticfiles.finders.TemporaryFileUploadHandler',
]
想了解更多,read this
问题不在代码中,我在 Postman 中缺少必需的 headers:
Content-Disposition: attachment; filename=upload.jpg;
无法在 API 视图中序列化或保存上传的图像。 如果保存,则文件已损坏且无法打开。如果序列化,得到: {"avatar":["No file was submitted."]} 请求内容:
MultiValueDict: {u'name': [<TemporaryUploadedFile: Avatar.jpg (image/jpeg)>]}
设置:
FILE_UPLOAD_HANDLERS = [
'django.core.files.uploadhandler.TemporaryFileUploadHandler',
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
网址:
urlpatterns = [
url(r'^test/$', TestReturn.as_view(), name='test-detail'),
url(r'^users/$', UserProfileAll.as_view(), name='userprofile-detail'),
url(r'^avatar/', UploadAvatar.as_view(), name='images')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
型号:
class AvatarStorage(models.Model):
avatar = models.ImageField(verbose_name="Uploaded image", upload_to='media', null=True)
连载:
class AvatarStorageSerializer(serializers.ModelSerializer):
avatar = serializers.ImageField(use_url=True)
class Meta:
model = AvatarStorage
fields = '__all__'
观看次数:
class UploadAvatar(APIView):
parser_classes = (MultiPartParser,)
def post(self, request, format=None):
serializer = AvatarStorageSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
对于发布,我使用的是 Postman,在那里设置:Body=>Form-Data 并从磁盘中选择一个文件。
如果这很重要,这是 API 移动版
更新设置文件 FILE_UPLOAD_HANDLERS 以点赞
FILE_UPLOAD_HANDLERS=[
'django.contrib.staticfiles.finders.MemoryFileUploadHandler',
'django.contrib.staticfiles.finders.TemporaryFileUploadHandler',
]
想了解更多,read this
问题不在代码中,我在 Postman 中缺少必需的 headers:
Content-Disposition: attachment; filename=upload.jpg;