[Answered ]-Which is the file word referring to here in this code

1👍

Well you just need to specify that field in your post request and also make sure to add the file name and id in your HTML to indicate the different fields :

class FileUploadView(APIView):   
        permission_classes = (IsAuthenticated,)
        parser_classes = (MultiPartParser, FormParser,)
    
    def post(self, request, *args, **kwargs):
        request.data['owner'] = request.user.id
        file_serializer = FileSerializer(data=request.data)
        if request.data['file'] is None:
            return Response({"error": "No File Found"},
                            status=status.HTTP_400_BAD_REQUEST
        if file_serializer.is_valid():
            data = self.request.data.get('file')

        if request.data['file_2'] is None:
             return Response({"error": "No File Found"},
                            status=status.HTTP_400_BAD_REQUEST
        if file_serializer.is_valid():
            data = self.request.data.get('file_2')

In your HTML make sure to add the field name and id so the files can be uploaded to it right fields.

# Example 

<form method="POST" action="."  enctype="multipart/form-data" multiple>
                {% csrf_token %}
<input type="file" name="file" id="id_file"  accept="image/*">
<input type="file" name="file_2" id="id_file_2"  accept="image/*">

<button type="submit" class="button bg-gray-700"> Upload Files </button>
</form>
👤Godda

Leave a comment