Django JSON响应错误状态 [英] Django JSON response error status
本文介绍了Django JSON响应错误状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的API正在返回JSON对象错误,但状态代码为 HTTP 200
:
My API is returning a JSON object on error but the status code is HTTP 200
:
response = JsonResponse({'status': 'false', 'message': message})
return response
如何更改响应代码以指示错误?
How can I change the response code to indicate an error?
推荐答案
code> JsonResponse 通常返回 HTTP 200
,这是'OK'的状态代码
。为了表示错误,您可以向 JsonResponse
添加HTTP状态代码,因为它是 HttpResponse
的子类:
JsonResponse
normally returns HTTP 200
, which is the status code for 'OK'
. In order to indicate an error, you can add an HTTP status code to JsonResponse
as it is a subclass of HttpResponse
:
response = JsonResponse({'status':'false','message':message}, status=500)
这篇关于Django JSON响应错误状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文