我一生无法弄清楚是什么原因造成的。我有一个使用Django
的小型django-rest-framework
项目。我正在尝试将令牌关键字从Token
修改为Bearer
以进行令牌认证。尽管找不到我的类继承自rest_framework.authentication.TokenAuthentication
,但由于某种原因而无法找到它。虽然可以正确找到该文件,所以我确定它不是路径问题。
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 20,
'DEFAULT_AUTHENTICATION_CLASSES': (
'core.auth.api.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
}
api.py
文件; from rest_framework.authentication import TokenAuthentication as RestFrameworkTokenAuthentication
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.views import ObtainAuthToken as RestFrameworkObtainAuthToken
from rest_framework.response import Response
class ObtainAuthToken(RestFrameworkObtainAuthToken):
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data,
context={'request': request})
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
token, created = Token.objects.get_or_create(user=user)
return Response({
'token': token.key,
'user_id': user.pk,
'username': user.username
})
class TokenAuthentication(RestFrameworkTokenAuthentication):
keyword = 'Bearer'
(env) karuhanga@sheer-aesthete:~/Documents/Projects/Log.Realty/backend/src$ python manage.py makemigrations
Traceback (most recent call last):
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/rest_framework/settings.py", line 184, in import_from_string
return getattr(module, class_name)
AttributeError: module 'core.auth.api' has no attribute 'TokenAuthentication'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 19, in <module>
execute_from_command_line(sys.argv)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/base.py", line 350, in execute
self.check()
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/base.py", line 366, in _run_checks
return checks.run_checks(**kwargs)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/urls/resolvers.py", line 396, in check
for pattern in self.url_patterns:
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/src/src/urls.py", line 21, in <module>
path('', include('core.urls.urls')),
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/src/core/urls/urls.py", line 4, in <module>
path('api/', include('core.urls.api.urls')),
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
(env) karuhanga@sheer-aesthete:~/Documents/Projects/Log.Realty/backend/src$ python manage.py makemigrations
Traceback (most recent call last):
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/rest_framework/settings.py", line 184, in import_from_string
return getattr(module, class_name)
AttributeError: module 'core.auth.api' has no attribute 'TokenAuthentication'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 19, in <module>
execute_from_command_line(sys.argv)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/base.py", line 350, in execute
self.check()
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/management/base.py", line 366, in _run_checks
return checks.run_checks(**kwargs)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/urls/resolvers.py", line 396, in check
for pattern in self.url_patterns:
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/src/src/urls.py", line 21, in <module>
path('', include('core.urls.urls')),
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/src/core/urls/urls.py", line 4, in <module>
path('api/', include('core.urls.api.urls')),
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/src/core/urls/api/urls.py", line 3, in <module>
from core.auth.api import ObtainAuthToken
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/src/core/auth/api.py", line 3, in <module>
from rest_framework.authtoken.views import ObtainAuthToken as RestFrameworkObtainAuthToken
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/rest_framework/authtoken/views.py", line 6, in <module>
from rest_framework.schemas import ManualSchema
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/rest_framework/schemas/__init__.py", line 32, in <module>
authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/rest_framework/settings.py", line 227, in __getattr__
val = perform_import(val, attr)
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/rest_framework/settings.py", line 172, in perform_import
return [import_from_string(item, setting_name) for item in val]
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/rest_framework/settings.py", line 172, in <listcomp>
return [import_from_string(item, setting_name) for item in val]
File "/home/karuhanga/Documents/Projects/Log.Realty/backend/env/lib/python3.6/site-packages/rest_framework/settings.py", line 187, in import_from_string
raise ImportError(msg)
ImportError: Could not import 'core.auth.api.TokenAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. AttributeError: module 'core.auth.api' has no attribute 'TokenAuthentication'.