How to send post request to a RestAPI using APIView and from the django forms

I have been trying to save the data entered in django forms directly into database using rest api,
i have a serious doubt on this topic.

@login_required
def add_member(request):
    if request.method == 'POST':
        form = GymMembersForm(request.POST, request.FILES)
        if form.is_valid():
            data = form.cleaned_data
            data['created_by'] = request.user.id
            data['created_at'] = timezone.now().isoformat() 
            
            #cleaning data
            # Convert model instances to their primary keys
            data['sub_type'] = data['sub_type'].id
            data['pay_method'] = data['pay_method'].id

            print(request.FILES)
            
        
            print(data)
            
            LOCAL_HOST = "http://127.0.0.1:8000/"
            csrf_token = get_token(request)
            headers = {'X-CSRFToken': csrf_token}
            
            response = requests.post(
                f'{LOCAL_HOST}api/members/',
                # Replace with your actual API URL
                json=data,
                headers=headers,
                cookies=request.COOKIES
            )
            
            print("***********")
            print(response.status_code)
            
            
            if response.status_code == 201:
                if 'user_profile_image' in request.FILES:
                    user_profile_image = request.FILES['user_profile_image']
                    file_path = default_storage.save('members/' + user_profile_image.name, user_profile_image)
                    print(file_path)
                messages.success(request, 'User created successfully!')  # Redirect to a success page
            else:
                # form.add_error(None, response.json())
                messages.error(request, 'Failed to create user. Please try again.')
        
    else:
        form = GymMembersForm()
        
        
    return render(request, 'members/add_members.html', {'form': form})

this is my logic for add_members.html,

def post(self,request,format=None):
        data = request.data
        data['user_profile_image'] = 'members/' + data['user_profile_image']
        serializer = GymMembersSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            if 'user_profile_image' in request.FILES:
                    user_profile_image = request.FILES['user_profile_image']
                    file_path = default_storage.save('members/' + user_profile_image.name, user_profile_image)
                    print(file_path)
            messages.success(request, 'User created successfully!') 
            return Response(serializer.data,status=201)
        print(serializer.errors)
        return Response(serializer.errors,status=400)

post request using APIView

but what i really want is to save the data directly to database without the server acting? i am wrong here?

Welcome @adwaithpj !

You are correct that it is unnecessary (and “wasteful”) to call another endpoint to save your data.

I suggest you review the docs and examples at: