简体   繁体   English

Allauth request.user 在 APIView 中是 AnonymousUser,但在 View 中 is_authenticated

[英]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',
    )
}

Taken from here取自这里

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.

相关问题 AbstractUser 上的 request.user 给出 AnonymousUser TypeError - request.user on AbstractUser gives AnonymousUser TypeError Django oauth2 request.user返回AnonymousUser - Django oauth2 request.user returns AnonymousUser Django request.user在第三方重定向后成为AnonymousUser - Django request.user become AnonymousUser after third party redirect 无法使用django rest框架进行身份验证(request.user = AnonymousUser) - Not able to authenticate using the django rest framework (request.user = AnonymousUser) 身份验证问题 - request.user 和 request.user.is_authenticated 未设置 - Authentication issues - request.user and request.user.is_authenticated is not set 第一个 django rest api 调用返回 request.user 作为 AnonymousUser 但进一步调用返回正确的用户 - First django rest api call returns request.user as AnonymousUser but further calls return proper user 登录门户后,在我的 Django 门户中,我的 request.user 始终提供值 AnonymousUser - In my Django portal after login to the portal my request.user is always giving the value AnonymousUser Django / Python-帮助移动is_authenticated和user == model.user进行查看 - Django/Python - Help moving is_authenticated and user == model.user to view 将request.user保存到Django额外视图 - Save request.user to an django extra view 为何IsAuthenticated的DRF源代码会在请求。用户不是布尔值时返回`request.user和request.user.is_authenticated()`? - How come in the DRF source code for IsAuthenticated, it returns `request.user and request.user.is_authenticated()` when request.user is not a boolean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM