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
    queryset=User.objects.all()

    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:
         model=User
         fields=('email','password') 

      

My url:

(r'^login/$',LoginView.as_view())

      

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?

+3


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.

RetrieveAPIView

is used for get

, and get

means getting data and getting required data pk

.



Use a different class to handle POST

+1


source







All Articles