Created
May 29, 2013 08:46
-
-
Save jineshpaloor/5668873 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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