Noor ul ain Ibrahim
Noor ul ain Ibrahim

Reputation: 105

Why I am getting empty phone number and password empty?

I am using django restframework for my Login view. I am getting empty phone and password Although I am passing the json via POSTMAN.

class LoginView(APIView):
    
    permission_classes = (permissions.AllowAny,)

    def post(self,request):
        phone = request.POST.get('phone')
        password = request.POST.get('password')
        
        if phone and password:
            get_user= CustomUser.objects.filter(phone=phone).first()
            username = get_user.username
            user = authenticate(request, username=username, password=password)
            if user is not None:
                login(request, user)
                return Response({"message": "Your are logged In!"}, status=status.HTTP_200_OK)
            else:
                return Response({"message": "Either phone number or password is wrong!"}, status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response({"message": "Please type again!"}, status=status.HTTP_400_BAD_REQUEST)  

The json I am passing using POSTMAN

{
    "phone": "923337195234",
    "password": "123456789"
}

Here are the results

{
    "message": "Please type again!"
}

If I comment out the first if-else statement

class LoginView(APIView):
    
    permission_classes = (permissions.AllowAny,)

    def post(self,request):
        phone = request.POST.get('phone')
        password = request.POST.get('password')
        
        # if phone and password:
        get_user= CustomUser.objects.filter(phone=phone).first()
        username = get_user.username
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return Response({"message": "Your are logged In!"}, status=status.HTTP_200_OK)
        else:
            return Response({"message": "Either phone number or password is wrong!"}, status=status.HTTP_400_BAD_REQUEST)
        # else:
        #     return Response({"message": "Please type again!"}, status=status.HTTP_400_BAD_REQUEST)

I am getting this result. Why phone and password is empty?

AttributeError: 'NoneType' object has no attribute 'username'

here is my urls.py

 path('login/', LoginView.as_view(), name="user_login")

Upvotes: 1

Views: 61

Answers (1)

Damoiskii
Damoiskii

Reputation: 1370

The json payload from your frontend should end up in request.data. What you should try is:

phone = request.data.get('phone')
password = request.data.get('password')

You could probably do a bit of reading here as well.

Upvotes: 1

Related Questions