pythondjangodjango-rest-frameworkdjango-rest-framework-jwtdjango-rest-framework-simplejwt

Could not import 'rest_framework_simplejwt.auth.JWTAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'


I am trying to make REST APIs using Django. I have installed djangorestframework & djangorestframework-simplejwt using pip

pip install djangorestframework
pip install djangorestframework-simplejwt

Whenever I import anything from rest_framework, It gives me this error

ImportError: Could not import 'rest_framework_simplejwt.auth.JWTAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ModuleNotFoundError: No module named 'rest_framework_simplejwt.auth'

Requirement.txt file:

djangorestframework==3.12.4
djangorestframework-simplejwt==4.7.2

Setting.py file:

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    'rest_framework',
    'rest_framework_simplejwt',
    'rest_framework_simplejwt.token_blacklist',
    "config",
    "response",
    "utils",
    "authentication",
]


REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.auth.JWTAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ]
}

SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(days=1),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=5),
}


Solution

  • Change auth to authentication. So it should be:

    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': [
            'rest_framework_simplejwt.authentication.JWTAuthentication',
        ]
        ...