Last active
January 4, 2016 08:39
-
-
Save vstoykov/8596778 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
try: | |
import json | |
except ImportError: | |
from django.utils import simplejson as json | |
from django.http import HttpResponse | |
from django.conf import settings | |
from django.core.serializers.json import DjangoJSONEncoder | |
JSONContentType = 'application/json; charset=%s' % (settings.DEFAULT_CHARSET, ) | |
class JSONResponse(HttpResponse): | |
""" | |
Response that will return JSON serialized value of the content | |
""" | |
INDENT = 1 if settings.DEBUG else None | |
def __init__(self, content, content_type=JSONContentType, *args, **kwargs): | |
json_content = json.dumps(content, cls=DjangoJSONEncoder, indent=self.INDENT) | |
super(JSONResponse, self).__init__(json_content, content_type) | |
def render_json(request, data, content_type=JSONContentType, *args, **kwargs): | |
""" | |
View shortcut around JSONResponse that will set the content_type | |
to text/plain if browser (like IE 8) does not support application/json | |
""" | |
if 'application/json' in content_type: | |
if 'application/json' not in request.META['HTTP_ACCEPT']: | |
content_type = "text/plain; charset=%s" % (settings.DEFAULT_CHARSET, ) | |
return JSONResponse(data, content_type, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment