Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return response message with 400 bad request status code from django restframwork?

Code in Python Django:

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

code in javascript

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

It returns the code when it is 200 ok, but returning nothing when it is 400 bad request. In fact, nothing seems replying message except 200_ok code. Is there any solution for that?

like image 250
Shures Nepali Avatar asked Nov 01 '25 12:11

Shures Nepali


1 Answers

Try with:

return Response(status=HTTP_400_BAD_REQUEST, data=error)

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

like image 193
Christian Bouvier Avatar answered Nov 04 '25 01:11

Christian Bouvier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!