0

Here is my code, that let users input their ID and Name to check if they are in a list of something.

def some_api(requests):
    try: 
        **do something**
        return HttpResponse('Your name is in the list') #Response 302 CODE FOUND NOT 202
    except:
        return JsonResponse([{ ID       : 123,         #Response 203 CODE EMPTY NOT 202
                               Name     : ABC,         #with information for users to double check
                               Content  : []           #their params
                              }])             

As always, when I return HttpResponse and JsonResponse, it is always the 200 CODE

2 Answers 2

3

With django-rest-framework you can add status like below:

from rest_framework import status
from rest_framework.response import Response

@api_view()
def some_api(request):
  try:
     **do something**
     return Response('Your name is in the list', status=status.HTTP_302_FOUND)
  except:
    return Response({"id": 123, "Name": "Abc", "Content": []}, 
                     status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION)
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to use the JsonResponse as said in your question.

JsonResponse({'status':'false','message':message}, status=500)

For HttpResponse, just use one of its subclasses: https://docs.djangoproject.com/en/3.1/ref/request-response/#httpresponse-subclasses.

Note that if you're using DRF, you should go for Ochom Richard's answer, rest_framework.response.Response object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.