Reputation: 2298
I performed this test:
class IsAuthenticatedView(APIView):
def get(self, request):
print(request.user)
return Response({
"is_authenticated": "true" if request.user.is_authenticated else "false"
}, 200)
and
class IsAuthenticatedView(View):
def get(self, request):
print(request.user)
return Response({
"is_authenticated": "true" if request.user.is_authenticated else "false"
}, 200)
The second one fails to load properly because of an AssertionError. However, the request.user changes among these two, where the APIView prints an AnonymousUser, the second prints the actual user logged in.
I'm using the Facebook login authentication.
Upvotes: 0
Views: 1467
Reputation: 644
Putting the answer here because it looks messy as a comment:
Have you set DEFAULT_AUTHENTICATION_CLASSES
in settings? You may want this:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
)
}
Taken from here
Upvotes: 1
Reputation: 140
You can just try request.user.is_authenticated as it is. Why do you want to try it in api view and view? Please explain.
Edit: Okay. Actually you must first use request.user.is_authenticated and then only use request.user And yes, there is no need to add () in is_authenticated bcoz it is not a function anymore in django 1.11. It is an attribute.
Upvotes: 0