# How to use token generated in api view

**URL:** <https://forum.djangoproject.com/t/how-to-use-token-generated-in-api-view/10262>\
**Category:** Using Django\
**Created:** [October 27, 2021, 4:09am UTC](https://forum.djangoproject.com/t/how-to-use-token-generated-in-api-view/10262 "2021-10-27T04:09:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CalvinTheCat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calvinthecat/32/5343_2.png) [@CalvinTheCat](https://forum.djangoproject.com/u/CalvinTheCat)\
**Post date:** [October 27, 2021, 4:09am UTC](https://forum.djangoproject.com/t/how-to-use-token-generated-in-api-view/10262/1 "2021-10-27T04:09:17Z")

</div>

I am using knox and not rest\_framework.authtoken. I am also able to generate tokens right now but I am having issue in using it. According to the documentation ([Authentication - Django REST framework](https://www.django-rest-framework.org/api-guide/authentication/)), an api function view requires

```auto
@authentication_classes([SessionAuthentication, BasicAuthentication])
@permission_classes([IsAuthenticated])

```

I replace Session and Basic to TokenAuthentication but I am getting the error of `NameError: name 'authentication_classes' is not defined`

In my views according to the documentation, i have imported the following:

```auto
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated

```

---

<div class="post-metadata">

**Author:** ![CalvinTheCat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calvinthecat/32/5343_2.png) [@CalvinTheCat](https://forum.djangoproject.com/u/CalvinTheCat)\
**Post date:** [October 27, 2021, 6:15am UTC](https://forum.djangoproject.com/t/how-to-use-token-generated-in-api-view/10262/2 "2021-10-27T06:15:38Z")

</div>

Found out I am missing some imports and I edited but now it is having some error when I am using the token.

In the views:

```auto
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import authentication_classes, permission_classes

@api_view(['GET'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def device_list(request):
    devices = Device.objects.all()
    deviceserializer = DeviceSerializers(devices, many = True)
    return Response(deviceserializer.data)

```

I am using [https://studygyaan.com/django/django-rest-framework-tutorial-register-login-logout](https://studygyaan.com/django/django-rest-framework-tutorial-register-login-logout) as a guide on how to generate the key and then using it in one of my api view but it can’t seem to work. Am I using it wrongly?

Here is the traceback when i tried to send in the Get request with the token

```auto
Traceback (most recent call last):
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\views.py", line 497, in dispatch
    self.initial(request, *args, **kwargs)
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\views.py", line 414, in initial
    self.perform_authentication(request)
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\views.py", line 324, in perform_authentication
    request.user
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\request.py", line 227, in user
    self._authenticate()
  File "c:\python39\lib\contextlib.py", line 135, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\request.py", line 78, in wrap_attributeerrors
    raise exc.with_traceback(info[2])
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\request.py", line 74, in wrap_attributeerrors
    yield
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\request.py", line 227, in user
    self._authenticate()
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\request.py", line 380, in _authenticate
    user_auth_tuple = authenticator.authenticate(self)
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\authentication.py", line 196, in authenticate
    return self.authenticate_credentials(token)
  File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\rest_framework\authentication.py", line 202, in authenticate_credentials
    except model.DoesNotExist:
rest_framework.request.WrappedAttributeError: type object 'Token' has no attribute 'DoesNotExist'

```
