Pipsqweek
Pipsqweek

Reputation: 404

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?

Upvotes: 0

Views: 150

Answers (2)

zaidfazil
zaidfazil

Reputation: 9235

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

Upvotes: 2

travis smith
travis smith

Reputation: 7

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.

Upvotes: 0

Related Questions