Gary Franks
Gary Franks

Reputation: 103

Django Knox-Rest Authentication Receives 500 Error on Login

I'm working on setting up a Django knox-rest framework for my rest api. I'm using Postman to test the login and after executing the request, my API returns a 500 error along with a stack dump. The dump returned to Postman is showing me the following error:

AttributeError at /users/api/auth/login
'LoginSerializer' object has no attribute 'validated_data'

a snippet from my api.py file:

class LoginAPI(generics.GenericAPIView):
    serializer_class = LoginSerializer
    permission_classes = ()

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.valiated_data
        _, token = AuthToken.objects.create(user)
        return Response({
            "user": UserSerializer(user, context=self.get_serializer_context()).data,
            "token": token
        })

the snippet from my serializers.py file:


class LoginSerializer(serializers.Serializer):
    username = serializers.CharField()
    password = serializers.CharField()

    def validate(self, data):
        user = authenticate(**data)
        if user and user.is_active:
            return user
        raise serializers.ValidationError("Incorrect Credentials")

The server starts clean and no errors are logged to the console other than the Post request and the 500 error. I have to go to Postman to see the dump with the above error.

I've tried just about everything I can think of or find to try without getting it figured out. So any help would be appreciated.

Upvotes: 2

Views: 259

Answers (1)

David Paredes
David Paredes

Reputation: 21

token = AuthToken.objects.create(user)[1] should work in your case.

Upvotes: 0

Related Questions