Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Allauth request.user在APIView中是匿名用户,但在视图中经过身份验证_Python_Django_Django Rest Framework_Django Allauth - Fatal编程技术网

Python Allauth request.user在APIView中是匿名用户,但在视图中经过身份验证

Python Allauth request.user在APIView中是匿名用户,但在视图中经过身份验证,python,django,django-rest-framework,django-allauth,Python,Django,Django Rest Framework,Django Allauth,我做了这个测试: class IsAuthenticatedView(APIView): def get(self, request): print(request.user) return Response({ "is_authenticated": "true" if request.user.is_authenticated else "false" }, 200) 及 由于断言错误,第二个无法正确加载。但

我做了这个测试:

class IsAuthenticatedView(APIView):
    def get(self, request):
        print(request.user)

        return Response({
            "is_authenticated": "true" if request.user.is_authenticated else "false"
        }, 200)

由于断言错误,第二个无法正确加载。但是,request.user在这两个选项中有所不同,其中APIView打印一个匿名用户,第二个打印实际登录的用户


我正在使用Facebook登录身份验证。

您可以尝试request.user.is\u按原样进行身份验证。为什么要在api视图和视图中尝试它?请解释一下

编辑: 可以实际上,您必须首先使用request.user.is_进行身份验证,然后只使用request.user
是的,在is_认证bcoz中不需要添加()它在django 1.11中不再是函数。这是一个属性。

将答案放在这里,因为它作为注释看起来很混乱:


您是否在设置中设置了
默认\u身份验证\u类
?您可能需要:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': ( 
        'rest_framework.authentication.SessionAuthentication',
    )
}

取自

request.user.is\u authenticated()
缺少
()
这不是问题,即使没有(),它仍然可以工作。您是否在设置中设置了
默认的\u AUTHENTICATION\u类
?您可能需要以下内容:
REST\u FRAMEWORK={'DEFAULT\u AUTHENTICATION\u CLASSES':('REST\u FRAMEWORK.AUTHENTICATION.SessionAuthentication',)}
取自
is\u authenticated
是一种方法。如果您尝试
bool(已验证)
它将始终返回
True
。因此,如果在代码中使用
if request.user.is\u authenticated
,它将始终是
True
。您需要使用
()
调用该方法以获得预期的结果。第三条注释是解决方案。非常感谢。APIView用于REST API。视图用于标准渲染视图。
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': ( 
        'rest_framework.authentication.SessionAuthentication',
    )
}