Uploading Multiple Files using Django rest framework without using Forms
Uploading Multiple Files using Django rest framework without using Forms
I'm trying to Post, from Postman, multiple files to my django app. I'm not using Forms, and there isn't a UI aspect to my app. Here is a my view class.
class FileUploader(APIView):
'''
Rest API for FileUploader
'''
permission_classes = (AllowAny,)
parser_classes = (MultiPartParser, )
@csrf_exempt
def post(self, request):
retval = Response(request.data, status=status.HTTP_201_CREATED)
logger.info('New post with the following data: '.format(request.data))
With this it says, "TypeError: init() missing 3 required positional arguments: 'META', 'input_data', and 'upload_handlers'"
If I use FormView, my Post has three keys, two represent files, the last is a string. During debugging my request has no field Data, and FILES is empty, and the POST doesn't have any information. Any pointers would be appreciated. I can upload more if that helps.
It's not a duplicate because he was able to upload multiple files and mine doesn't upload any files. I'm struggling to figure out how to find the files within the request and since they aren't there how to set up the views (and not the serialize) to receive multiple files.
enter image description here
moreover, you are not returning any response from your view class
– JPG
Sep 18 '18 at 15:37
I'm not concerned about making the Model from the request, so serialization isn't part of the problem, the problem is the physical POST request. Later in the View I return the response either 201 or a depending. What I'm struggling with is having both files contained within the request.
– Nathan Smith
Sep 18 '18 at 16:17
added an answer, try it
– JPG
Sep 18 '18 at 16:29
2 Answers
2
Write a view class as
from rest_framework.views import APIView
from rest_framework.response import Response
class FileUploader(APIView):
'''
Rest API for FileUploader
'''
permission_classes = (AllowAny,)
def post(self, request, *args, **kwargs):
files_list = request.FILES
data = request.data
return Response(data="files": " files uploaded".format(len(files_list)),
"data": " data included".format(len(data)))
and send it using form-data
in POSTMAN
form-data
When I made these changes, data was a field, but had len 0 and FILES was also zero. So response was ""files": "0 files uploaded", "data": "0 data included"
– Nathan Smith
Sep 18 '18 at 16:35
How did you add the files and data? screenshot please
– JPG
Sep 18 '18 at 16:37
Just added the screenshot
– Nathan Smith
Sep 18 '18 at 16:38
And the header is Key = Content-Type Value = multipart/form-data
– Nathan Smith
Sep 18 '18 at 16:40
remove the header and try again
– JPG
Sep 18 '18 at 16:41
change the above code to like below and include header 'Content-Type': 'multipart/form-data'
in the request.
'Content-Type': 'multipart/form-data'
class FileUploader(APIView):
'''
Rest API for FileUploader
'''
permission_classes = (AllowAny,)
parser_classes = (MultiPartParser, )
@csrf_exempt
def post(self, request, *args, **kwargs):
print(request.data)
return Response("message": "success")
When I made the change, it failed saying "AttributeError: 'NoneType' object has no attribute 'decode'"
– Nathan Smith
Sep 18 '18 at 16:28
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Possible duplicate of Django REST: Uploading and serializing multiple images
– JPG
Sep 18 '18 at 15:34