Я использую Django Rest Framework с permission_classes
для создания API. Насколько я понимаю, после перехода на этот API с неаутентифицированным пользователем я должен быть перенаправлен в форму входа, верно? Вместо этого я получаю следующую ошибку:
WrappedAttributeError at /sessions/api/listPatients/
'IsAdminUser' object has no attribute 'authenticate'
Нет перенаправления на форму входа ... и я не понимаю, почему.
Почему это происходит? Что я делаю неправильно? Как это исправить?
Вот это urls.py
:
path('api/getSessions/',
views.GetSessions.as_view(),
name="GetSessionsAPI"),
А вот точка зрения, на которую я пытаюсь перейти:
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
а вот еще один выводит ошибку:
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
Вот полный ответ:
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:
Эта ошибка исчезает, когда я меняю следующую строку в файле настроек base.py:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.permissions.IsAdminUser', # <- commenting out solves the problem!
),
Тем не менее, строка была добавлена после книги Two Scoops of Django ...