首页 > 解决方案 > 'str' 对象在 djangorestframework_simplejwt 上没有属性 'decode'

问题描述

我试图从带有链接https://django-rest-framework-simplejwt.readthedocs.io/en/latest/getting_started.html的 djangorestframework-simplejwt 文档中遵循这个快速入门

但是我在尝试获取令牌时遇到问题,并且总是返回此错误 'str' object has no attribute 'decode'

在此处输入图像描述

编辑: 这是我在 urls.py 上的代码

from django.contrib import admin
from django.urls import path
from rest_framework_simplejwt import views as jwt_views
from core.views import HelloView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/token/', jwt_views.TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', jwt_views.TokenRefreshView.as_view(), name='token_refresh'),
    path('hello/', HelloView.as_view(), name='hello'),
]

设置.py

...
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
]
...
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
}
...

视图.py

from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated


class HelloView(APIView):
    permission_classes = (IsAuthenticated,)

    def get(self, request):
        content = {'message': 'Hello, World!'}
        return Response(content)

标签: djangodjango-rest-frameworkjwt

解决方案


降级 PyJWT 为我完成了这项工作。

为此,请将您的 requirements.txt 中的相应行更改为

PyJWT==v1.7.1

推荐阅读