'Why I can't log in with this django login code, the message says: "Authentication credentials were not provided"

This is my views.py:

LoginView(APIView):
    authentication_classes = [SessionAuthentication, BasicAuthentication]
    permission_classes = [IsAuthenticated]
        
    def post(self, request):
        if request.method == 'POST':
            email = request.data.get('email', None)
            print(email)
            password = request.data.get('password',None)
            print(password)
            user = authenticate(request, email= "email", password= "pasword")
            print(user)
        if  user : 
            login(request, user)
            print("punto1")
            return Response(status=status.HTTP_200_OK)
    
        return Response(
            status=status.HTTP_404_NOT_FOUND)

The output is:

403 Forbiden
{
  "detail": "Authentication credentials were not provided."
}


Solution 1:[1]

it will work if you change permission_classes = [AllowAny]

it will allow any user to use your API

also you need to import it from rest_framework.permissions import AllowAny

but it is better to authorize yourself, these are links for documentation of basic and session authentication

https://www.django-rest-framework.org/api-guide/authentication/#sessionauthentication

https://www.django-rest-framework.org/api-guide/authentication/#basicauthentication

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 oruchkin