
Django rest framework authentication error “ TypeError: 'type' object is not iterable ”
Last updated 1 year, 1 month ago | 2292 views 75 5

Django rest framework authentication error “ TypeError: 'type' object is not iterable ”
This error comes because "authentication_classes" expecting an iterable(list/tuple) but somehow it is getting something else. So it needs to check a few things to resolve this issue.
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/local/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view return self.dispatch(request, *args, **kwargs) File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 492, in dispatch request = self.initialize_request(request, *args, **kwargs) File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 394, in initialize_request authenticators=self.get_authenticators(), File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 272, in get_authenticators return [auth() for auth in self.authentication_classes] TypeError: 'type' object is not iterable
authentication scheme with multiple classes
authentication scheme in setting file
# List
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
]
}
# OR
# Tuple
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
}
per-view or per-viewset basis
# authentication_classes defined in List
authentication_classes = [SessionAuthentication, BasicAuthentication]
# OR
# authentication_classes defined in Tuple
authentication_classes = (SessionAuthentication, BasicAuthentication)
authentication scheme with a single class
if you are using only one authentication class then with the tuple assignment put a "comma" at the end otherwise the value is treated as a string which raised the error "TypeError: 'type' object is not iterable"
# Tuple | notice that after 'rest_framework_jwt.authentication.JSONWebTokenAuthentication'
# there is a "," comma
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
}
# OR
# List | comma can be ignored
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_jwt.authentication.JSONWebTokenAuthentication'
],
}
per-view or per-viewset basis
# Tuple | notice that after 'authentication.TokenAuthentication' there is a "," comma
authentication_classes = (authentication.TokenAuthentication,)
# OR
# List | comma can be ignored
authentication_classes = [authentication.TokenAuthentication]