Skip to content

Instantly share code, notes, and snippets.

@vstoykov
Last active January 4, 2016 08:39

Revisions

  1. vstoykov revised this gist Feb 21, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions django_json_response.py
    Original 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 need set correct
    contenty type for older version of IE.
    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
  2. vstoykov revised this gist Feb 21, 2014. 2 changed files with 34 additions and 34 deletions.
    34 changes: 0 additions & 34 deletions django_ajax_response.py
    Original file line number Diff line number Diff line change
    @@ -1,34 +0,0 @@
    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)
    34 changes: 34 additions & 0 deletions django_json_response.py
    Original 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)
  3. vstoykov created this gist Jan 24, 2014.
    34 changes: 34 additions & 0 deletions django_ajax_response.py
    Original 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)