【发布时间】:2021-07-09 09:24:28
【问题描述】:
我试图按照 djangorestframework-simplejwt 文档的快速入门链接https://django-rest-framework-simplejwt.readthedocs.io/en/latest/getting_started.html
但我在尝试获取令牌时遇到问题,并且总是返回此错误
'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'),
]
settings.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',
],
}
...
views.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)
【问题讨论】:
-
您需要添加您的代码。查看官方文档不会帮助找到错误
-
好的,我会编辑这个@Neeraj
-
你在哪里使用jwt生成的token进行认证? @MasDimas
标签: django django-rest-framework jwt