Huangquan-Creek 2023-10-18 21:23 采纳率: 76%
浏览 6
已结题

simple-jwt

利用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

  • 写回答

2条回答 默认 最新

  • Leodong. 2023-10-18 21:33
    关注

    该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
    根据报错信息,问题出在jwt_token()函数的参数列表中多了一个未知的关键字参数data。为了解决这个问题,你需要将data参数从函数定义中移除,并在函数内部使用request.data来获取请求数据。以下是修改后的代码:

    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
        }
    

    这样修改后,你的代码应该可以正常运行了。


    如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月31日
  • 创建了问题 10月18日

悬赏问题

  • ¥15 C++ 句柄后台鼠标拖动如何实现
  • ¥15 有人会SIRIUS 5.8.0这个软件吗
  • ¥30 comsol仿真等离激元
  • ¥15 静电纺丝煅烧后如何得到柔性纤维
  • ¥15 (标签-react native|关键词-镜像源)
  • ¥100 照片生成3D人脸视频
  • ¥15 伪装视频时长问题修改MP4的时长问题,
  • ¥15 JETSON NANO
  • ¥15 VS开发qt时如何在paintgl函数中用pushbutton控制切换纹理
  • ¥20 关于 openpyxl 处理excel文件地问题