我使用django-rest-auth 生成令牌以证明我的API,我在项目中配置得很好。
您看到我的快照右上角,我已使用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'
,则也会出现此错误。