How to create login view using django-rest-framework

I have a serializer and a view for my login like this.

class LoginView(generics.RetrieveAPIView):
    serializer_class= LoginSerializer

    error_messages = {
        'invalid': "Invalid username or password",
        'disabled': "Sorry, this account is suspended",

    def _error_response(self, message_key):
        data = {
            'success': False,
            'message': self.error_messages[message_key],
            'user_id': None,
    def post(self,request):
        email = request.POST.get('email')
        password = request.POST.get('password')
        user = authenticate(email=email, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)

                return Response(status=status.HTTP_100_OK)
            return self._error_response('disabled')
        return self._error_response('invalid')


and the serializer:

class LoginSerializer(serializers.ModelSerializer):
     class Meta:


My url:



When I run the code, I get an Incorrect Configured error   in / login / Expected LoginView to be called with a URL keyword argument named "pk". Correct the url conf or set the attribute .lookup_field

on the view correctly.

I currently have no redirection in my opinion. what I did wrong?


source to share

1 answer

You are using generic.RetrieveAPIView

which extends RetrieveModelMixin

and GenericAPIView

, according to the docs, pk

you need or you need to set .lookup_field

in your class.


is used for get

, and get

means getting data and getting required data pk


Use a different class to handle POST



All Articles