1

I use the django-rest-auth to generate the token to certificate my APIs, all I have configured fine in my project.

enter image description here

You see my snapshot top right corner, I have login my test01 account, using http://127.0.0.1:8000/rest-auth/login/(this is the django-rest-auth build-in link)

But when I access the url to KoleListAPIView, there comes out issue, says the request is 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'

My views.py is bellow:

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())

But I have login the test01 account.

And my REST_FRAMEWORK in settings:

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

If I remove the 'rest_framework.authentication.TokenAuthentication' in the REST_FRAMEWORK of settings, its also this Error.

4
  • You do not get me, I means I use the django-rest-auth provided the login link, I have login, why I request other API, I still get the AnonymousUser error? Commented Nov 14, 2017 at 7:53
  • Could you please update "DEFAULT_AUTHENTICATION_CLASSES" like this. REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.SessionAuthentication', ), in setting.py Commented Nov 14, 2017 at 8:02
  • 1
    @ABDULNIYASPM This works, thank you. Commented Nov 14, 2017 at 8:06
  • You can find the reason here Commented Nov 14, 2017 at 8:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.