Skip to content

Instantly share code, notes, and snippets.

@chriskief
Created October 24, 2013 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriskief/7131242 to your computer and use it in GitHub Desktop.
Save chriskief/7131242 to your computer and use it in GitHub Desktop.
import json
from django.http import HttpResponse
class JSONResponseMixin(object):
# a mixin that can be used to render a JSON response
response_class = HttpResponse
def render_to_response(self, context, **response_kwargs):
# returns a JSON response, transforming 'context' to make the payload
response_kwargs['content_type'] = 'application/json'
return self.response_class(self.convert_context_to_json(context), **response_kwargs)
def convert_context_to_json(self, context):
# this would need to be a bit more complex if we're dealing with more complicated variables
return json.dumps(context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment