Django Rest Framework Not Returning Anything

Sorry im new to Django, I have weird case where function not returning anything that the function suppose to return Response rest framework to clientt.

class AccountValidationView(APIView):
    def put(self, request):
        token = str(request.POST.get('token')).strip()
        token = token.split('.')
        password = str(request.POST.get('password'))
        confirm_password = str(request.POST.get('confirm_password')).strip()
        
        validation_password(password, confirm_password)
        # if valid process data validation

def validation_password(password, confirm):
     if len(password) == 0:
        message = {
            'password': 'Password cannot be empty'
        }
        
        return Response(message, status=status.HTTP_400_BAD_REQUEST)

    if password != confirm:
        message = {
            'confirm_password': 'Confirm password not match with the password you given'
        }
        
        return Response(message, status=status.HTTP_400_BAD_REQUEST)

the validation_password function not returning anything so is straight to processing data. what do I do wrong here. My validation_password not returning response when error occurred.

Where you are calling this API. Like in postman or in your front-end.

Currently using ajax now for web admin

Okay, so first thing in your def put(self, request): only validation_password is returning the Response what if it passes validation, have you return anything.
Also can you share your ajax method as well.

One thing I noticed is that you are getting your post data like this

if you want to get your data then instead of that you should use

password = request.data['password']

Any data in your request can be processed within request.data

Thanks for your reply, it help me to understand what is going on

Response what if it passes validation, have you return anything.

Yes, just bunch of serializer

serializer = AccountValidationSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.create(validated_data=data)
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Also can you share your ajax method as well.

this my ajax

$('#login').on('click', function() {
            let methodType = 'PUT',
            cookie = document.cookie;
            csrfToken = cookie.substring(cookie.indexOf('=') + 1),
            token = $('#token').val(),
            password = $('#password').val(),
            confirm_password = $('#confirm-password').val();

            $('#password').removeClass('is-invalid');
            $('#confirm-password').removeClass('is-invalid');
            $('#pesan').html('');

            $.ajax({
                headers: { "X-CSRFToken": csrfToken },
                url: '/web_admin/user/api/account_validation/',
                type: methodType,
                data: {
                    token : token,
                    password : password,
                    confirm_password : confirm_password
                },
                success: function (data, status, xhr) {
                    console.log(data);
                    location.reload();
                },
                error: function (jqXhr, textStatus, errorMessage) {
                    console.log(jqXhr);
                    $('#html').html(jqXhr.responseText)

                    json_response = jqXhr.responseJSON;

                    if(typeof json_response !== 'undefined') {
                        let pesan_error = '';

                        if(typeof json_response.email !== 'undefined') {
                            pesan_error += json_response.email + '<br>';
                        }

                        if(typeof json_response.user !== 'undefined') {
                            pesan_error += json_response.user + '<br>';
                        }

                        if(typeof json_response.password !== 'undefined') {
                            $('#password').addClass('is-invalid');
                            pesan_error += json_response.password + '<br>';
                        }

                        if(typeof json_response.confirm_password !== 'undefined') {
                            $('#confirm-password').addClass('is-invalid');
                            pesan_error += json_response.confirm_password + '<br>';
                        }

                    }
                }
            });
        });

Okay, put some print statements to check if you are getting what you want in your API’s view.

Also, what are you getting in these 2 console logs here

Okay, put some print statements to check if you are getting what you want in your API’s view.

That the weird part, it got print in the terminal. but no return response. Its confusing

Also, what are you getting in these 2 console logs here

Its still on develope now, i just want to know what server send back to client. I will erase it, if can solve this problem

No need to remove, You are right if server sends the response to client then it should be printed in you browser’s console.

No need to remove, You are right if server sends the response to client then it should be printed in you browser’s console.

Its straight to get response from serializer that save the input. If password and confirm password not equal it suppose to return :

{'confirm_password':'Confirm password not match with the password you given'}

But it get save to database. That what im looking for right now. It suppose to return the error message on validation_password() function if error occured. But it didnt.

Okay, so I’ve updated your view and validation method

    def put(self, request):
        ...
        password = request.data['password']
        confirm_password = request.data['confirm_password']
        
        message = validation_password(password, confirm_password)
        if message:
            return Response(message, status=status.HTTP_400_BAD_REQUEST)
        # Other code that you want to execute after validation_password
def validation_password(password, confirm):
    message = None
    if len(password) == 0:
        message = {
            'password': 'Password cannot be empty'
        }
        
        return message

    if password != confirm:
        message = {
            'confirm_password': 'Confirm password not match with the password you given'
        }
        
        return message

Try this it will work.

thank your so much man, appreciate it

No problem, just let me know if it works :crossed_fingers:.

No problem, just let me know if it works :crossed_fingers:.

it works thank you so much.
But what really happened here, why it not returned any response. Its really bugging me out :slight_smile:

As much I know, it is because in this def validation_password(password, confirm) method request is not given and to return a response or to get a request from client it is required.
Whereas in def put(self, request) this method request is being passed and after checking here that a message have anything Response was returned.