[Answered ]-Unable to use Django Authenticate

2👍

Firstly you should not use the name of the view as authenticate as it is getting mixed with Django auth’s authenticate
And second, you don’t need self parameter in authentication view i.e. def authenticate_user(request)
Update your code like this:

VIEW

from django.contrib.auth import authenticate

@csrf_exempt
@api_view(['POST'])
def authenticate_user(request):
    print(request.data)
    user = authenticate(username=request.data['username'], password=request.data['password'])
    if user is not None:
        return Response({'Status': 'Authenticated'})
    else:
        return Response({'Status': 'Unauthenticated'})

And your urls.py will be updated as per the view’s name:

URLS

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'create/', views.create),
    url(r'retrieve/', views.retrieve),
    url(r'auth/', views.authenticate_user),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

Leave a comment