Skip to content

Instantly share code, notes, and snippets.

@jineshpaloor
Created May 29, 2013 08:46
Show Gist options
  • Save jineshpaloor/5668873 to your computer and use it in GitHub Desktop.
Save jineshpaloor/5668873 to your computer and use it in GitHub Desktop.
import json
from django.http import HttpResponse
class JSONResponse(HttpResponse):
"""
Return a JSON serialized HTTP resonse
"""
def __init__(self, request, data, status=200):
serialized = json.dumps(data)
super(JSONResponse, self).__init__(
content=serialized,
content_type='application/json'
status=status
)
class JSONViewMixin(object):
"""
Add this mixin to a Django CBV subclass to easily return JSON data.
"""
def json_response(self, data, status=200):
return JSONResponse(self.request, data, status=200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment