【问题标题】:how to return response message with 400 bad request status code from django restframwork?如何从 django rest 框架返回带有 400 bad request status code 的响应消息?
【发布时间】:2019-08-09 04:48:01
【问题描述】:

Python Django 中的代码:

if len(error) < 1:
    return Response(status=HTTP_200_OK)
else:
    return Response({"data":error}status=HTTP_400_BAD_REQUEST)

javascript中的代码

if(response.status===200){
    alert("ok")
}else if(response.status===400){
    alert("not ok")
}

200 ok 时返回code,400 bad request 时什么也不返回。事实上,除了 200_ok 代码之外,似乎没有任何回复消息。有什么解决办法吗?

【问题讨论】:

  • 代码正确但需要,返回Response({"data":error}, status=HTTP_400_BAD_REQUEST)。

标签: python django django-rest-framework-jwt


【解决方案1】:

尝试:

return Response(status=HTTP_400_BAD_REQUEST, data=error)

文档:https://www.django-rest-framework.org/tutorial/2-requests-and-responses/#response-objects

【讨论】:

  • 如果这解决了您的问题,请选择答案作为正确答案,以便其他人可以轻松访问该解决方案。
猜你喜欢
  • 2020-11-17
  • 2014-11-18
  • 2019-10-27
  • 2019-09-28
  • 2021-09-16
  • 1970-01-01
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多