0

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?

Selcuk
  • 57,004
  • 12
  • 102
  • 110
Pipsqweek
  • 404
  • 4
  • 19

2 Answers2

1

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

Community
  • 1
  • 1
zaidfazil
  • 9,017
  • 2
  • 24
  • 47
  • Other than a little convenience, does it offer any greater efficiency? Reading the docs I get the impression its just the http response altered a bit. – Pipsqweek May 16 '17 at 01:26
  • The little convenience can even be counted as bit more efficient? – zaidfazil May 16 '17 at 01:28
0

Additionally, Json dumps returns a simply the json representation of the data you pass in so if that's your goal then this method is adequate, but if you need to include any headers then you could simply pass in params and list into JSONResponse with any additional arguments while minimizing load.