WrappedAttributeError 'IsAdminUser' object has no attribute 'authenticate'
up vote
0
down vote
favorite
I'm using Django Rest Framework with permission_classes
to create an API. My understanding is that once I go to that API with an unauthenticated user I should be redirected to the login form, right? Instead, I get the following error:
WrappedAttributeError at /sessions/api/listPatients/
'IsAdminUser' object has no attribute 'authenticate'
There's no redirect to the login form... and I don't understand why.
Why is this happening? What am I doing wrong? How to fix that?
Here's the urls.py
:
path('api/getSessions/',
views.GetSessions.as_view(),
name="GetSessionsAPI"),
And here's the view that I'm trying to go to:
class GetSessions(generics.ListCreateAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = SessionSerializer
def get_queryset(self):
if self.request.user.is_authenticated:
return Session.objects.filter(
patient__created_by_user=self.request.user)
else:
raise PermissionDenied
and here's the other one raising the error:
class ListPatients(generics.ListAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = PatientsSerializer
def get_queryset(self):
if self.request.user.is_authenticated:
try:
return Patient.objects.filter(
created_by_user=self.request.user).
filter(curently_active=True)
except Patient.DoesNotExist:
raise PermissionDenied
else:
raise PermissionDenied
Here's the full traceback:
WrappedAttributeError at /sessions/api/listPatients/
'IsAdminUser' object has no attribute 'authenticate'
Request Method: GET
Request URL: http://localhost:8000/sessions/api/listPatients/
Django Version: 2.0.5
Exception Type: WrappedAttributeError
Exception Value:
'IsAdminUser' object has no attribute 'authenticate'
Exception Location: /Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate, line 375
Python Executable: /Users/karol/Development/ZS_2_0/ZS_2_0_venv/bin/python
Python Version: 3.6.4
Python Path:
['/Users/karol/Development/ZS_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/patient',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/services',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/notes',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/practice',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/activity_logger',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/session',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/my_calendar',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/session/tests',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0/users',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python36.zip',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages',
'/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0']
Server time: Sat, 17 Nov 2018 15:29:56 +0100
Traceback Switch to copy-and-paste view
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
yield ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate
user_auth_tuple = authenticator.authenticate(self) ...
▶ Local vars
During handling of the above exception ('IsAdminUser' object has no attribute 'authenticate'), another exception occurred:
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/exception.py in inner
response = get_response(request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in inner
return func(*args, **kwds) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/views/decorators/csrf.py in wrapped_view
return view_func(*args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/views/generic/base.py in view
return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
response = self.handle_exception(exc) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in handle_exception
self.raise_uncaught_exception(exc) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
self.initial(request, *args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in initial
self.perform_authentication(request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in perform_authentication
request.user ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in __exit__
self.gen.throw(type, value, traceback) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
six.reraise(type(exc), exc, info[2]) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/utils/six.py in reraise
raise value.with_traceback(tb) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
yield ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate
user_auth_tuple = authenticator.authenticate(self) ...
▶ Local vars
UPDATE:
This error goes away when I change the following line in my base.py settings file:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.permissions.IsAdminUser', # <- commenting out solves the problem!
),
However, the line has been added following the book Two Scoops of Django ...
django django-rest-framework django-authentication
|
show 3 more comments
up vote
0
down vote
favorite
I'm using Django Rest Framework with permission_classes
to create an API. My understanding is that once I go to that API with an unauthenticated user I should be redirected to the login form, right? Instead, I get the following error:
WrappedAttributeError at /sessions/api/listPatients/
'IsAdminUser' object has no attribute 'authenticate'
There's no redirect to the login form... and I don't understand why.
Why is this happening? What am I doing wrong? How to fix that?
Here's the urls.py
:
path('api/getSessions/',
views.GetSessions.as_view(),
name="GetSessionsAPI"),
And here's the view that I'm trying to go to:
class GetSessions(generics.ListCreateAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = SessionSerializer
def get_queryset(self):
if self.request.user.is_authenticated:
return Session.objects.filter(
patient__created_by_user=self.request.user)
else:
raise PermissionDenied
and here's the other one raising the error:
class ListPatients(generics.ListAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = PatientsSerializer
def get_queryset(self):
if self.request.user.is_authenticated:
try:
return Patient.objects.filter(
created_by_user=self.request.user).
filter(curently_active=True)
except Patient.DoesNotExist:
raise PermissionDenied
else:
raise PermissionDenied
Here's the full traceback:
WrappedAttributeError at /sessions/api/listPatients/
'IsAdminUser' object has no attribute 'authenticate'
Request Method: GET
Request URL: http://localhost:8000/sessions/api/listPatients/
Django Version: 2.0.5
Exception Type: WrappedAttributeError
Exception Value:
'IsAdminUser' object has no attribute 'authenticate'
Exception Location: /Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate, line 375
Python Executable: /Users/karol/Development/ZS_2_0/ZS_2_0_venv/bin/python
Python Version: 3.6.4
Python Path:
['/Users/karol/Development/ZS_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/patient',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/services',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/notes',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/practice',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/activity_logger',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/session',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/my_calendar',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/session/tests',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0/users',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python36.zip',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages',
'/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0']
Server time: Sat, 17 Nov 2018 15:29:56 +0100
Traceback Switch to copy-and-paste view
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
yield ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate
user_auth_tuple = authenticator.authenticate(self) ...
▶ Local vars
During handling of the above exception ('IsAdminUser' object has no attribute 'authenticate'), another exception occurred:
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/exception.py in inner
response = get_response(request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in inner
return func(*args, **kwds) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/views/decorators/csrf.py in wrapped_view
return view_func(*args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/views/generic/base.py in view
return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
response = self.handle_exception(exc) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in handle_exception
self.raise_uncaught_exception(exc) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
self.initial(request, *args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in initial
self.perform_authentication(request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in perform_authentication
request.user ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in __exit__
self.gen.throw(type, value, traceback) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
six.reraise(type(exc), exc, info[2]) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/utils/six.py in reraise
raise value.with_traceback(tb) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
yield ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate
user_auth_tuple = authenticator.authenticate(self) ...
▶ Local vars
UPDATE:
This error goes away when I change the following line in my base.py settings file:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.permissions.IsAdminUser', # <- commenting out solves the problem!
),
However, the line has been added following the book Two Scoops of Django ...
django django-rest-framework django-authentication
2
Looks like error occures in another view:/sessions/api/listPatients/
. Can you show it also?
– neverwalkaloner
Nov 17 at 14:21
@neverwalkaloner - yes - that must be it, but I don't see what ... Added like you suggested. Any hints?
– user1544500
Nov 17 at 14:28
Can you add full traceback?
– neverwalkaloner
Nov 17 at 14:30
@neverwalkaloner - added.
– user1544500
Nov 17 at 14:33
1
You're certainly misreading the book. Authentication is not the same as permissions.
– Daniel Roseman
Nov 17 at 15:06
|
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm using Django Rest Framework with permission_classes
to create an API. My understanding is that once I go to that API with an unauthenticated user I should be redirected to the login form, right? Instead, I get the following error:
WrappedAttributeError at /sessions/api/listPatients/
'IsAdminUser' object has no attribute 'authenticate'
There's no redirect to the login form... and I don't understand why.
Why is this happening? What am I doing wrong? How to fix that?
Here's the urls.py
:
path('api/getSessions/',
views.GetSessions.as_view(),
name="GetSessionsAPI"),
And here's the view that I'm trying to go to:
class GetSessions(generics.ListCreateAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = SessionSerializer
def get_queryset(self):
if self.request.user.is_authenticated:
return Session.objects.filter(
patient__created_by_user=self.request.user)
else:
raise PermissionDenied
and here's the other one raising the error:
class ListPatients(generics.ListAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = PatientsSerializer
def get_queryset(self):
if self.request.user.is_authenticated:
try:
return Patient.objects.filter(
created_by_user=self.request.user).
filter(curently_active=True)
except Patient.DoesNotExist:
raise PermissionDenied
else:
raise PermissionDenied
Here's the full traceback:
WrappedAttributeError at /sessions/api/listPatients/
'IsAdminUser' object has no attribute 'authenticate'
Request Method: GET
Request URL: http://localhost:8000/sessions/api/listPatients/
Django Version: 2.0.5
Exception Type: WrappedAttributeError
Exception Value:
'IsAdminUser' object has no attribute 'authenticate'
Exception Location: /Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate, line 375
Python Executable: /Users/karol/Development/ZS_2_0/ZS_2_0_venv/bin/python
Python Version: 3.6.4
Python Path:
['/Users/karol/Development/ZS_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/patient',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/services',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/notes',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/practice',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/activity_logger',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/session',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/my_calendar',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/session/tests',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0/users',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python36.zip',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages',
'/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0']
Server time: Sat, 17 Nov 2018 15:29:56 +0100
Traceback Switch to copy-and-paste view
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
yield ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate
user_auth_tuple = authenticator.authenticate(self) ...
▶ Local vars
During handling of the above exception ('IsAdminUser' object has no attribute 'authenticate'), another exception occurred:
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/exception.py in inner
response = get_response(request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in inner
return func(*args, **kwds) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/views/decorators/csrf.py in wrapped_view
return view_func(*args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/views/generic/base.py in view
return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
response = self.handle_exception(exc) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in handle_exception
self.raise_uncaught_exception(exc) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
self.initial(request, *args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in initial
self.perform_authentication(request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in perform_authentication
request.user ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in __exit__
self.gen.throw(type, value, traceback) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
six.reraise(type(exc), exc, info[2]) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/utils/six.py in reraise
raise value.with_traceback(tb) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
yield ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate
user_auth_tuple = authenticator.authenticate(self) ...
▶ Local vars
UPDATE:
This error goes away when I change the following line in my base.py settings file:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.permissions.IsAdminUser', # <- commenting out solves the problem!
),
However, the line has been added following the book Two Scoops of Django ...
django django-rest-framework django-authentication
I'm using Django Rest Framework with permission_classes
to create an API. My understanding is that once I go to that API with an unauthenticated user I should be redirected to the login form, right? Instead, I get the following error:
WrappedAttributeError at /sessions/api/listPatients/
'IsAdminUser' object has no attribute 'authenticate'
There's no redirect to the login form... and I don't understand why.
Why is this happening? What am I doing wrong? How to fix that?
Here's the urls.py
:
path('api/getSessions/',
views.GetSessions.as_view(),
name="GetSessionsAPI"),
And here's the view that I'm trying to go to:
class GetSessions(generics.ListCreateAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = SessionSerializer
def get_queryset(self):
if self.request.user.is_authenticated:
return Session.objects.filter(
patient__created_by_user=self.request.user)
else:
raise PermissionDenied
and here's the other one raising the error:
class ListPatients(generics.ListAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = PatientsSerializer
def get_queryset(self):
if self.request.user.is_authenticated:
try:
return Patient.objects.filter(
created_by_user=self.request.user).
filter(curently_active=True)
except Patient.DoesNotExist:
raise PermissionDenied
else:
raise PermissionDenied
Here's the full traceback:
WrappedAttributeError at /sessions/api/listPatients/
'IsAdminUser' object has no attribute 'authenticate'
Request Method: GET
Request URL: http://localhost:8000/sessions/api/listPatients/
Django Version: 2.0.5
Exception Type: WrappedAttributeError
Exception Value:
'IsAdminUser' object has no attribute 'authenticate'
Exception Location: /Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate, line 375
Python Executable: /Users/karol/Development/ZS_2_0/ZS_2_0_venv/bin/python
Python Version: 3.6.4
Python Path:
['/Users/karol/Development/ZS_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/patient',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/services',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/notes',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/practice',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/activity_logger',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/session',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/my_calendar',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/session/tests',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0/users',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python36.zip',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages',
'/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0',
'/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0']
Server time: Sat, 17 Nov 2018 15:29:56 +0100
Traceback Switch to copy-and-paste view
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
yield ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate
user_auth_tuple = authenticator.authenticate(self) ...
▶ Local vars
During handling of the above exception ('IsAdminUser' object has no attribute 'authenticate'), another exception occurred:
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/exception.py in inner
response = get_response(request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in inner
return func(*args, **kwds) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/views/decorators/csrf.py in wrapped_view
return view_func(*args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/views/generic/base.py in view
return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
response = self.handle_exception(exc) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in handle_exception
self.raise_uncaught_exception(exc) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
self.initial(request, *args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in initial
self.perform_authentication(request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in perform_authentication
request.user ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in __exit__
self.gen.throw(type, value, traceback) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
six.reraise(type(exc), exc, info[2]) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/utils/six.py in reraise
raise value.with_traceback(tb) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
yield ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
self._authenticate() ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate
user_auth_tuple = authenticator.authenticate(self) ...
▶ Local vars
UPDATE:
This error goes away when I change the following line in my base.py settings file:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.permissions.IsAdminUser', # <- commenting out solves the problem!
),
However, the line has been added following the book Two Scoops of Django ...
django django-rest-framework django-authentication
django django-rest-framework django-authentication
edited Nov 17 at 14:53
asked Nov 17 at 14:19
user1544500
65611022
65611022
2
Looks like error occures in another view:/sessions/api/listPatients/
. Can you show it also?
– neverwalkaloner
Nov 17 at 14:21
@neverwalkaloner - yes - that must be it, but I don't see what ... Added like you suggested. Any hints?
– user1544500
Nov 17 at 14:28
Can you add full traceback?
– neverwalkaloner
Nov 17 at 14:30
@neverwalkaloner - added.
– user1544500
Nov 17 at 14:33
1
You're certainly misreading the book. Authentication is not the same as permissions.
– Daniel Roseman
Nov 17 at 15:06
|
show 3 more comments
2
Looks like error occures in another view:/sessions/api/listPatients/
. Can you show it also?
– neverwalkaloner
Nov 17 at 14:21
@neverwalkaloner - yes - that must be it, but I don't see what ... Added like you suggested. Any hints?
– user1544500
Nov 17 at 14:28
Can you add full traceback?
– neverwalkaloner
Nov 17 at 14:30
@neverwalkaloner - added.
– user1544500
Nov 17 at 14:33
1
You're certainly misreading the book. Authentication is not the same as permissions.
– Daniel Roseman
Nov 17 at 15:06
2
2
Looks like error occures in another view:
/sessions/api/listPatients/
. Can you show it also?– neverwalkaloner
Nov 17 at 14:21
Looks like error occures in another view:
/sessions/api/listPatients/
. Can you show it also?– neverwalkaloner
Nov 17 at 14:21
@neverwalkaloner - yes - that must be it, but I don't see what ... Added like you suggested. Any hints?
– user1544500
Nov 17 at 14:28
@neverwalkaloner - yes - that must be it, but I don't see what ... Added like you suggested. Any hints?
– user1544500
Nov 17 at 14:28
Can you add full traceback?
– neverwalkaloner
Nov 17 at 14:30
Can you add full traceback?
– neverwalkaloner
Nov 17 at 14:30
@neverwalkaloner - added.
– user1544500
Nov 17 at 14:33
@neverwalkaloner - added.
– user1544500
Nov 17 at 14:33
1
1
You're certainly misreading the book. Authentication is not the same as permissions.
– Daniel Roseman
Nov 17 at 15:06
You're certainly misreading the book. Authentication is not the same as permissions.
– Daniel Roseman
Nov 17 at 15:06
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
Turned out it was my mistake which got uncovered thanks to the comments under my question:
By mistake I have added 'rest_framework.permissions.IsAdminUser',
to DEFAULT_AUTHENTICATION_CLASSES
instead of DEFAULT_PERMISSION_CLASSES
inside my settings file.
That solved the problem - thanks! :)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Turned out it was my mistake which got uncovered thanks to the comments under my question:
By mistake I have added 'rest_framework.permissions.IsAdminUser',
to DEFAULT_AUTHENTICATION_CLASSES
instead of DEFAULT_PERMISSION_CLASSES
inside my settings file.
That solved the problem - thanks! :)
add a comment |
up vote
0
down vote
Turned out it was my mistake which got uncovered thanks to the comments under my question:
By mistake I have added 'rest_framework.permissions.IsAdminUser',
to DEFAULT_AUTHENTICATION_CLASSES
instead of DEFAULT_PERMISSION_CLASSES
inside my settings file.
That solved the problem - thanks! :)
add a comment |
up vote
0
down vote
up vote
0
down vote
Turned out it was my mistake which got uncovered thanks to the comments under my question:
By mistake I have added 'rest_framework.permissions.IsAdminUser',
to DEFAULT_AUTHENTICATION_CLASSES
instead of DEFAULT_PERMISSION_CLASSES
inside my settings file.
That solved the problem - thanks! :)
Turned out it was my mistake which got uncovered thanks to the comments under my question:
By mistake I have added 'rest_framework.permissions.IsAdminUser',
to DEFAULT_AUTHENTICATION_CLASSES
instead of DEFAULT_PERMISSION_CLASSES
inside my settings file.
That solved the problem - thanks! :)
answered Nov 17 at 15:15
user1544500
65611022
65611022
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352071%2fwrappedattributeerror-isadminuser-object-has-no-attribute-authenticate%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Looks like error occures in another view:
/sessions/api/listPatients/
. Can you show it also?– neverwalkaloner
Nov 17 at 14:21
@neverwalkaloner - yes - that must be it, but I don't see what ... Added like you suggested. Any hints?
– user1544500
Nov 17 at 14:28
Can you add full traceback?
– neverwalkaloner
Nov 17 at 14:30
@neverwalkaloner - added.
– user1544500
Nov 17 at 14:33
1
You're certainly misreading the book. Authentication is not the same as permissions.
– Daniel Roseman
Nov 17 at 15:06