http.py 659 B

1234567891011121314151617181920212223
  1. import json
  2. from django.http import HttpResponse
  3. from django_browserid.util import LazyEncoder
  4. class JSONResponse(HttpResponse):
  5. """
  6. HttpResponse that takes in a list or dict and outputs JSON for the response
  7. body.
  8. """
  9. def __init__(self, data, status=200):
  10. """
  11. :param data:
  12. List or dict to serialize to JSON in the response body.
  13. :param status:
  14. HTTP status code to use for this response. Defaults to 200.
  15. """
  16. data_json = json.dumps(data, cls=LazyEncoder)
  17. super(JSONResponse, self).__init__(
  18. data_json, content_type='application/json', status=status)