django - 'str' 对象在 djangorestframework_simplejwt 上没有属性 'decode'
问题描述
但是我在尝试获取令牌时遇到问题,并且总是返回此错误
'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)
解决方案
降级 PyJWT 为我完成了这项工作。
为此,请将您的 requirements.txt 中的相应行更改为
PyJWT==v1.7.1
推荐阅读
- automation - 如何在 AutoHotkey 中制作一个代码,所有打开的程序都接收某个热键?
- laravel - Laravel Queue:多队列、多任务、多远程服务器
- c++ - 无法初始化 SDL2,但 SDL_GetError() 不返回任何内容
- algorithm - 什么是实现 APL 升级的高性能算法?
- node.js - 如何使用 Express Handlebars 模板引擎从 mongoDB 检索和更新图像?
- multithreading - 为什么 epoll 线程安全而不是 libevent
- javascript - 为什么使用模块时浏览器中的窗口无法访问类的实例?
- c# - 有没有办法从 AWS 开发工具包中检索 AWS 凭证信息?
- reactjs - 单击 Antd Tab REACT 时更改逻辑
- javascript - 使用javascript将具有大于2的数字序列的数组中的连续数字分组到子数组中