【问题标题】:Django rest framework POST request: JSONDecodeError at /competition-createDjango rest 框架 POST 请求:/competition-create 处的 JSONDecodeError
【发布时间】:2021-05-06 11:36:35
【问题描述】:

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

# 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__"

回复:

我尝试使用常规 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, *args, **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, *args, **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)

【问题讨论】:

  • 您是否尝试过使用self.request.data 而不是request.data 并重新检查您尝试发布的JSON?
  • @ShrAwanPoudel 这可悲的是什么也没做。
  • @OliverIlmjärv 能否请您添加您的 traceback 而不是错误图片?
  • @Mubasharjaved 编辑帖子

标签: python-3.x django django-rest-framework django-request


【解决方案1】:

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

# 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,
                status=status.HTTP_201_CREATED,
                content_type="json")

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

【讨论】:

  • 还是同样的问题,没有任何改变
  • 尝试将serializer.data 更改为competition.data
  • 同样的问题。但是我注意到在 if 语句中调用 is_valid() 时会引发错误。
  • 然后print(request.data) 看看你得到什么类型的数据。因为如果要反序列化的数据不是有效的 JSON,则会引发 JSONDecodeError 错误。
  • 找到了答案。这是我提交给 api 的字段之一。它有一个无法解析为 json 的字符
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 2023-03-16
    • 2016-04-07
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    相关资源
    最近更新 更多