urls.py里 #
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token #导包
urlpatterns = [
path(‘login/‘,obtain_jwt_token),
path(‘refresh/‘,refresh_jwt_token),
]
# settings.py 里
JWT_AUTH = {
‘JWT_AUTH_HEADER_PREFIX‘: ‘JWT‘,
‘JWT_EXPIRATION_DELTA‘: datetime.timedelta(days=1),
‘JWT_RESPONSE_PAYLOAD_HANDLER‘:
‘user.views.UserLogin‘, # 重新login登录返回函数
}
#views.py 里
#设置Token四行代码
def create_token(user):
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
return token
def UserLogin(token, user=None, request=None):
return {
"token": token,
"username": user.username,
"email": user.email
}
原文:https://www.cnblogs.com/aa1bb2/p/14088529.html