Last active
January 4, 2016 08:39
Revisions
-
vstoykov revised this gist
Feb 21, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,8 +15,8 @@ class JSONResponse(HttpResponse): """ Response that will return JSON serialized value of the content. It accept request as first argument because it needs to set correct content type for older versions of IE. """ INDENT = 1 if settings.DEBUG else None -
vstoykov revised this gist
Feb 21, 2014 . 2 changed files with 34 additions and 34 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,34 +0,0 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ 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 JSON_CONTENT_TYPE = 'application/json; charset=%s' % (settings.DEFAULT_CHARSET, ) JSON_FALLBACK_CONTENT_TYPE = "text/plain; charset=%s" % (settings.DEFAULT_CHARSET, ) class JSONResponse(HttpResponse): """ Response that will return JSON serialized value of the content. It accept request as first argument because it need set correct contenty type for older version of IE. """ INDENT = 1 if settings.DEBUG else None def __init__(self, request, content, *args, **kwargs): accept = request.META.get('HTTP_ACCEPT', '*/*') if 'text/html' in accept and 'application/json' not in accept: content_type = JSON_FALLBACK_CONTENT_TYPE else: content_type = JSON_CONTENT_TYPE json_content = json.dumps(content, cls=DjangoJSONEncoder, indent=self.INDENT) super(JSONResponse, self).__init__(json_content, content_type, *args, **kwargs) -
vstoykov created this gist
Jan 24, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ 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)