安装
pip install djangorestframework_simplejwt
设置
settings.py
设置:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}
# 默认鉴权用户,可更改
AUTH_USER_MODEL = "auth_user"
在用户的模型类里面,设置这个属性:USERNAME_FIELD = ‘username’,可以用来声明哪一个是 username 字段
路由配置:
from django.urls import path
from rest_framework_simplejwt import views as JWTAuthenticationViews
urlpatterns = [
path('api/token/', JWTAuthenticationViews.TokenObtainPairView.as_view(), name='get_token'),
path('api/token/refresh/', JWTAuthenticationViews.TokenRefreshView.as_view(), name='refresh_token'),
]
视图参考:
from rest_framework.views import APIV