gpt4 book ai didi

Django Rest 使用 JWT,获取 AttributeError : Invalid API setting: 'JWT_PAYLOAD_HANDLER'

转载 作者:行者123 更新时间:2023-12-02 03:18:07 25 4
gpt4 key购买 nike

我正在按照本教程( https://medium.com/@dakota.lillie/django-react-jwt-authentication-5015ee00ef9a )在 Django 中实现基于 JWT 的身份验证。但是,我不断收到我无法弄清楚的错误。

我向后查看返回的对象是否被使用。

序列化器类:

class UserSerializerWithToken(serializers.ModelSerializer):
token = serializers.SerializerMethodField()
password = serializers.CharField(write_only=True)

def get_token(self, obj):
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

payload = jwt_payload_handler(obj)
token = jwt_encode_handler(payload)
return token

def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance

class Meta:
model = User
fields = ('token','username','password')

查看相同内容:

class Users(APIView):
"""
Create new user.
"""

permission_classes = (permissions.AllowAny,)

def post(self, request, format=None):
serializer = UserSerializerWithToken(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

它应该返回 JSON 格式的 token ,但是我收到的错误是:

AttributeError at /api/users/

Invalid API setting: 'JWT_PAYLOAD_HANDLER'

Request Method: POST
Request URL: http://127.0.0.1:8000/api/users/
Django Version: 2.1.7
Exception Type: AttributeError
Exception Value:

Invalid API setting: 'JWT_PAYLOAD_HANDLER'

Exception Location: $/venv/lib/python3.7/site-packages/rest_framework/settings.py in __getattr__, line 216
Python Executable: $/venv/bin/python

用户已在我的数据库中创建,但它会引发 token 生成错误。我对此不是很专家。谢谢!

最佳答案

我遇到了同样的错误。我是从 rest_framework 导入 api_setting 而不是 rest_framework_jwt

关于Django Rest 使用 JWT,获取 AttributeError : Invalid API setting: 'JWT_PAYLOAD_HANDLER' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55538458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com