首页 > 解决方案 > 无法从 JWT 导入 TokenObtainPairView、TokenRefreshView

问题描述

我想使用 Json Web 令牌身份验证。

但是当我导入时,它给了我没有找到 TokenObtainPairView、TokenRefreshView 参考的错误,但是我安装了 jwt。

网址.py:

   from django.contrib import admin
   from django.urls import path
   from rest_framework_jwt.views import (
     TokenObtainPairView,
     TokenRefreshView,
   )
    from django.conf.urls import url,include

   urlpatterns = [
       path('admin/', admin.site.urls),
       path('api/token/', TokenObtainPairView.as_view(), 
       name='token_obtain_pair'),
       path('api/token/refresh/', TokenRefreshView.as_view(), 
       name='token_refresh'),
       url(r'^auth/', include('authsystem.urls'))

设置.py:

        REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
    'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework_simplejwt.authentication.JWTAuthentication',
    'rest_framework.authentication.SessionAuthentication',
),

}

当我做 pip freeze 时,我有包:

           Django==2.2.4
           django-cors-headers==3.1.0
           djangorestframework==3.10.2
           djangorestframework-jwt==1.11.0
           djangorestframework-simplejwt==4.3.0
           Pillow==6.1.0
           PyJWT==1.7.1
           pytz==2019.2
           sqlparse==0.3.0

我试图从不同的方式导入,但它仍然给我找不到参考。

标签: pythondjangodjango-rest-frameworkjwt-auth

解决方案


您从错误的框架导入它,您需要从rest_framework_simplejwt.views模块导入它,而不是rest_framework_jwt.views模块:

from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)

djangorestframework-jwt您同时安装and是否有特定原因djangorestframework-simplejwt


推荐阅读