微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Django 休息框架 POST 请求:/competition-create 处的 JSONDecodeError

如何解决Django 休息框架 POST 请求:/competition-create 处的 JSONDecodeError

我试图简单地执行一个发布请求,将新的竞争保存到数据库中。

# views.py

class CompetitionCreateView(generics.CreateAPIView):
    serializer_class = CompetitionSerializer
    queryset = Competition.objects.all()

    def create(self,request,*args,**kwargs):
        serializer = CompetitionSerializer(data=request.data)
        if serializer.is_valid():
            competition = serializer.save()

            return Response(
                data=CompetitionSerializer(competition).data,status=status.HTTP_201_CREATED,content_type="json")
        return Response(data=serializer.errors,status=status.HTTP_400_BAD_REQUEST,content_type="json")

# serializer.py



class CompetitionSerializer(serializers.ModelSerializer):

    class Meta:
        model = Competition
        fields = "__all__"

回复

enter image description here

我曾尝试使用常规 APIView 并从 def create(...) 切换到 def post(...),但没有成功。也尝试将请求数据内容解析为json。

这是来自控制台的回溯

Internal Server Error: /competition-create
test_deploy_web | Traceback (most recent call last):
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py",line 47,in inner
test_deploy_web |     response = get_response(request)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py",line 181,in _get_response
test_deploy_web |     response = wrapped_callback(request,*callback_args,**callback_kwargs)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/django/views/decorators/csrf.py",line 54,in wrapped_view
test_deploy_web |     return view_func(*args,**kwargs)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py",line 70,in view
test_deploy_web |     return self.dispatch(request,**kwargs)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py",line 509,in dispatch
test_deploy_web |     response = self.handle_exception(exc)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py",line 469,in handle_exception
test_deploy_web |     self.raise_uncaught_exception(exc)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py",line 480,in raise_uncaught_exception
test_deploy_web |     raise exc
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py",line 506,in dispatch
test_deploy_web |     response = handler(request,**kwargs)
test_deploy_web |   File "/app/view/views.py",line 21,in post
test_deploy_web |     if serializer.is_valid():
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py",line 220,in is_valid
test_deploy_web |     self._validated_data = self.run_validation(self.initial_data)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py",line 419,in run_validation
test_deploy_web |     value = self.to_internal_value(data)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py",line 476,in to_internal_value
test_deploy_web |     validated_value = field.run_validation(primitive_value)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/fields.py",line 568,in run_validation
test_deploy_web |     value = self.to_internal_value(data)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/fields.py",line 1899,in to_internal_value
test_deploy_web |     return self.model_field.to_python(data)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/django/contrib/postgres/fields/ranges.py",line 80,in to_python
test_deploy_web |     vals = json.loads(value)
test_deploy_web |   File "/usr/local/lib/python3.9/json/__init__.py",line 346,in loads
test_deploy_web |     return _default_decoder.decode(s)
test_deploy_web |   File "/usr/local/lib/python3.9/json/decoder.py",line 340,in decode
test_deploy_web |     raise JSONDecodeError("Extra data",s,end)
test_deploy_web | json.decoder.JSONDecodeError: Extra data: line 1 column 5 (char 4)

解决方法

实际上你正在做一些连线的事情。所以不要使用这个:

# here you are serializing the already serialized data
return Response(
                data=CompetitionSerializer(competition).data,status=status.HTTP_201_CREATED,content_type="json")

试试这个:

return Response(
                data=serializer.data,content_type="json")

希望这能解决您的问题;)

,

输入 JSON 有问题,没有被正确解析。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。