0

I am working on a project with the django rest framework. I created the registration view without any problem, but in the login view I have an issue: when I try to log in, the response returns "Invalid credentials"

view.py:

class ObtainAuthTokenView(APIView):
    authentication_classes = []
    permission_classes = []

    def post(self, request):
        context = {}

        email = request.POST.get('username')
        password = request.POST.get('password')
        account = authenticate(email=email, password=password)
        if account:
            try:
                token = Token.objects.get(user=account)
            except Token.DoesNotExist:
                token = Token.objects.create(user=account)
            context['response'] = 'Successfully authenticated.'
            context['pk'] = account.pk
            context['email'] = email.lower()
            context['token'] = token.key
        else:
            context['response'] = 'Error'
            context['error_message'] = 'Invalid credentials'

        return Response(context)

urls.py

urlpatterns = [
    path('login', ObtainAuthTokenView.as_view(), name="login"),
]

I used this data to post:

{"username":"[email protected]","password":"123456"}

After I post this data the response returns 'invalid credentials'. I found out that maybe the email and password after get the request are equal nun.

so why am I facing this issue?

1 Answer 1

1

The error was because I used the wrong identifier.

I wrote this:

request.POST.get()

instead of this:

request.data.get()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.