python - 导入错误 : Could not import 'rest_framework_simplejwt.authentication.JWTAuthentication'

标签 python django django-rest-framework

我正在尝试在 GCP 上部署 django 应用程序,但是当我尝试进行迁移时,它给了我这个错误:

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



设置.py
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated'
    ],
'DEFAULT_RENDERER_CLASSES': [
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ]
}

SIMPLE_JWT = {
    'ALGORITHM': 'HS256',
    'SIGNING_KEY': SECRET_KEY,
    'VERIFYING_KEY': None,
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=800),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=2),
}
OAUTH2_PROVIDER = {
        'ACCESS_TOKEN_EXPIRE_SECONDS': 60 * 15,
        'OAUTH_SINGLE_ACCESS_TOKEN': True,
        'OAUTH_DELETE_EXPIRED': True
 }

要求.txt
django-cors-headers
pyjwt
djangorestframework
djangorestframework-jwt==1.11.0

我错过了什么?

更新
我安装了 rest_framework_simplejwt ,现在错误转移到:

No module named 'rest_framework_simplejwt.tokens'

最佳答案

看来你混淆了两个包。 djangorestframework-jwt您在 requirements.txt 中的内容是 no longer maintained .它提供了 rest_framework_jwt.authentication.JSONWebTokenAuthentication认证类。

但是,您实际使用的,rest_framework_simplejwt.authentication.JWTAuthentication , 来自 pip 包 djangorestframework_simplejwt

所以你需要更新你的requirements.txt。删除 djangorestframework-jwt并添加 djangorestframework_simplejwt

关于python - 导入错误 : Could not import 'rest_framework_simplejwt.authentication.JWTAuthentication' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57864306/

相关文章:

Django Rest Framework 测试 API 响应断言

python - 颜色映射到蓝色阴影 - python

python - MyPy 参数默认值不兼容 - "Type[str]"与 "Callable[[str], obj]"

Python 临时文件 [Errno 66] 目录不为空

python - 如何在 Django REST Framework 中测试 FileField

django-axes 没有获取请求参数

python - 如何判断哪个 Keras 模型更好?

python - pytest:使用 re.escape() 断言转义字符失败

python - Django 上传和处理没有数据保留的文件

django - Pinax可以部署在GAE上吗?