[英]Allauth request.user is AnonymousUser in APIView, but is_authenticated in View
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.由于 AssertionError,第二个无法正确加载。 However, the request.user changes among these two, where the APIView prints an AnonymousUser, the second prints the actual user logged in.但是, request.user 在这两者之间发生变化,其中 APIView 打印 AnonymousUser,第二个打印实际登录的用户。
I'm using the Facebook login authentication.我正在使用 Facebook 登录身份验证。
Putting the answer here because it looks messy as a comment:将答案放在这里,因为它看起来像评论一样混乱:
Have you set DEFAULT_AUTHENTICATION_CLASSES
in settings?您是否在设置中设置了DEFAULT_AUTHENTICATION_CLASSES
? You may want this:你可能想要这个:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
)
}
You can just try request.user.is_authenticated as it is.您可以直接尝试 request.user.is_authenticated 。 Why do you want to try it in api view and view?为什么要在api视图和视图中尝试? 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.实际上你必须首先使用 request.user.is_authenticated 然后才使用 request.user 是的,没有必要在 is_authenticated bcoz 中添加 () 它在 django 1.11 中不再是一个函数。 It is an attribute.它是一个属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.