Search code examples
pythonjsondjangopolling

Most efficient / proper way to give Json response in Django?


I am writing a polling view in Django -- being called once per second. I'd like to avoid the effect of hammering the server (since its a small device).

Currently I'm returning this response:

return HttpResponse(json.dumps({'body':body}))

but is there a more appropriate way to do this, thus using minimal resources / features for this simple / ongoing response?


Solution

  • You could use JsonResponse,

    from django.http import JsonResponse
    
    return JsonResponse({'body':body})
    

    Then, you don't have to do json.dumps,

    For documentation, click here

    If you want you could refer to this question, Creating a JSON response using Django and Python