利用rest_framework_simplejwt自定义一个令牌。但是显示多了一个未知的参数。请求指点迷津,并改一下代码内容部分。
from rest_framework_simplejwt.tokens import Token
def jwt_token(token, user=None, request=None):
"""
自定义登陆成功后的响应函数,登陆后的数据处理函数
执行这个函数,账号和密码肯定是正确有效的了,手动校验验证码即可
:param token: Token
:param user: Logged-in user
:param request: Request object
:return: Login API response content, token
"""
param = request.data
uuid = param.get('uuid')
verify = param.get('verify')
if not uuid or not verify:
return {'msg': 'Please enter the captcha'}
# Get the saved captcha answer from redis and compare it with the passed result
cache = get_redis_connection(alias='verify_codes')
redis_verify = cache.get(LOGIN_KEY_TEMPLATE % uuid)
cache.delete(LOGIN_KEY_TEMPLATE % uuid)
if not redis_verify:
return {'msg': 'Captcha expired'}
if redis_verify.upper() != verify.upper():
return {'msg': 'Incorrect captcha'}
# Create a new token using simple_jwt's Token class and add custom claims
new_token = Token(token)
new_token['id'] = user.id
new_token['username'] = user.username
new_token['avatar'] = user.userdetail.avatar if hasattr(user, 'userdetail') else None
return {
'token': new_token,
'id': user.id,
'username': user.username,
'avatar': user.userdetail.avatar if hasattr(user, 'userdetail') else None
}
**_**下面是报错情况__
_
ERROR log 241 Internal Server Error: /users/login/
Traceback (most recent call last):
File "/home/hqc-001/.virtualenvs/drf9/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/hqc-001/.virtualenvs/drf9/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/hqc-001/.virtualenvs/drf9/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
return view_func(*args, **kwargs)
File "/home/hqc-001/.virtualenvs/drf9/lib/python3.10/site-packages/django/views/generic/base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
File "/home/hqc-001/.virtualenvs/drf9/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/hqc-001/.virtualenvs/drf9/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/hqc-001/.virtualenvs/drf9/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/hqc-001/.virtualenvs/drf9/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/hqc-001/.virtualenvs/drf9/lib/python3.10/site-packages/rest_framework_simplejwt/views.py", line 41, in post
serializer = self.get_serializer(data=request.data)
File "/home/hqc-001/.virtualenvs/drf9/lib/python3.10/site-packages/rest_framework/generics.py", line 110, in get_serializer
return serializer_class(*args, **kwargs)
TypeError: jwt_token() got an unexpected keyword argument 'data'
ERROR basehttp 212 "POST /users/login/ HTTP/1.1" 500 95622