0

Is it possible to add server response code to JsonResponse ? I need server to reply with 404 in some circumstances.

I have following view

def CreateOrAuth(request):
try: 
    username = request.POST.get("username")

    queryset = User.objects.get(username=username)

except Exception as e:
    return JsonResponse({'status': 'user with {} not exist'.format(username)})

And I want to add 404 server code here

Alexey K
  • 6,537
  • 18
  • 60
  • 118

1 Answers1

3

Yes, you can. Just pass additional parameter status to JsonResponse:

return JsonResponse({'status': 'user with {} not exist'.format(username)}, status=404)
GwynBleidD
  • 20,081
  • 5
  • 46
  • 77