'How show error message " submitted data is not a string"

i want to show the error message ....when the submitted data is not an encoded string for example passing a image file that time show error message invalid.

here my code

class UploadViewSet(APIView):
permission_classes = (AllowAny,)
 
def post(self, request, *args, **kwargs):
    file = request.POST.get("content")
    if file is not None:
        try:
            base64.b64decode(file)
        except ValueError:
            return Response({"error": "invalid"})
        data = StringIO(file)
        reader = csv.DictReader(data, delimiter=',')  
        print(reader)
        for row in reader:
            new_company = Company(
                name=row['name'],
                hr_name=row['hr_name'],
                hr_email=row['hr_email'],
                hr_verified=row['hr_verified'],
                user_id=row['user_id'],
                primary_phone=row['primary_phone'],
                comments=row['comments'],
                )
            new_company.save()
            print(new_company)
            return Response({"status": "success"},status.HTTP_201_CREATED)
            
    else:
        return Response({"error": "field required"})


Solution 1:[1]

Try:

if data is not an encoded string:
    return Response({"Failure": "message_to_show"},status=status.HTTP_400_BAD_REQUEST)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ranu Vijay