django - 无法为 API 设置 'rest_framework_jwt.authentication.JSONWebTokenAuthentication' 导入 'DEFAULT_AUTHENTICATION_CLASSES'

标签 django django-rest-framework django-views jwt django-authentication

完全错误: 无法为 API 设置“DEFAULT_AUTHENTICATION_CLASSES”导入“rest_framework_jwt.authentication.JSONWebTokenAuthentication”。 ImportError: 无法从 'django.utils.encoding' 导入名称 'smart_text'

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ),
}

这是虚拟环境中的点卡住:

(backend) PS D:\js\backend> pip freeze                          
asgiref==3.5.1
Django==4.0.4
django-cors-headers==3.11.0
djangorestframework==3.13.1
djangorestframework-jwt==1.11.0
djangorestframework-simplejwt==5.1.0
mysqlclient==2.1.0
PyJWT==1.7.1
pytz==2022.1
sqlparse==0.4.2
tzdata==2022.1

在错误的中间,它为装饰器解决了views.py中的一些行:

from http.client import HTTPResponse
from multiprocessing import context
from django.shortcuts import render
from django.http import HttpResponse, Http404, JsonResponse
from .models import Tweet
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import api_view, permission_classes
from rest_framework import status

我不确定它们是否相关

最佳答案

'rest_framework_jwt.authentication.JSONWebTokenAuthentication' 这是由 djangorestframework-jwt 提供的,不再维护。只需卸载它 而是使用 'rest_framework_simplejwt.authentication.JWTAuthentication' 来自 djangorestframework-simplejwt

1 - 安装 djangorestframework-simplejwt : pip install djangorestframework-simplejwt

2- 你的 'DEFAULT_AUTHENTICATION_CLASSES' 应该是这样的:

'DEFAULT_AUTHENTICATION_CLASSES': (
    
    'rest_framework.authentication.SessionAuthentication',
    'rest_framework.authentication.BasicAuthentication',
    'rest_framework_simplejwt.authentication.JWTAuthentication', 
),

3 - 在您的根 urls.py 文件(或任何其他 url 配置)中,包含 Simple JWT 的 TokenObtainPairView 的路由>TokenRefreshView 浏览量:

from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)

urlpatterns = [
    ...
    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
    ...
]

更多信息请查看official documentation

关于django - 无法为 API 设置 'rest_framework_jwt.authentication.JSONWebTokenAuthentication' 导入 'DEFAULT_AUTHENTICATION_CLASSES',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72102911/

相关文章:

Django 测试 - 发送包含整数的数组数组

python - 从模板获取views.py中的值

django - celery 在哪里存储任务函数?

python - 当使用 django 进行 API 调用时,Django REST Framework 的好处是什么?

rest - 为用户管理定义 REST 端点的正确方法

使用 through 从 ManyToManyField 进行 Django 序列化

python - 如何使用数据库值在 django 中创建自定义表单

python - 如何获取UpdateView中当前的对象?

django - 未找到 uWSGI 命令

python - 如何使用 django Rest-auth View 和自定义 html 模板而不是可浏览的 API