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:
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 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
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'