使用django-rest-auth

时间:2017-11-14 07:33:37

标签: python django django-rest-framework django-rest-auth

我使用django-rest-auth 生成令牌以证明我的API,我在项目中配置得很好。

enter image description here

您看到我的快照右上角,我已使用test01登录我的http://127.0.0.1:8000/rest-auth/login/帐户(这是django-rest-auth内置链接)

但是,当我访问KoleListAPIView的网址时,会出现问题,请求是AnonymousUser

AttributeError at /api/userproducts/kole/list/
'AnonymousUser' object has no attribute 'openstackcloud_id'
Request Method: GET
Request URL:    http://localhost:8000//api/userproducts/kole/list/
Django Version: 1.11.5
Exception Type: AttributeError
Exception Value:    
'AnonymousUser' object has no attribute 'kole_id'

我的views.py如下:

class KoleListAPIView(ListAPIView):
    """
    list the Kole
    """
    serializer_class = KoleListSerializer
    def list(self, request, *args, **kwargs):

        kole_id = self.request.user.kole_id  # there comes the issue.
        if kole_id == None:
            return Response(data="please enter the password again", status=HTTP_511_NETWORK_AUTHENTICATION_REQUIRED, exception=Exception())

但我已登录test01帐户。

我的REST_FRAMEWORK设置:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES':[], #'rest_framework.permissions.IsAuthenticated'
    'DEFAULT_AUTHENTICATION_CLASSES':['rest_framework.authentication.TokenAuthentication'],
    'PAGE_SIZE':10
}

如果我删除设置的REST_FRAMEWORK中的'rest_framework.authentication.TokenAuthentication',则也会出现此错误。

0 个答案:

没有答案