Skip to content

Instantly share code, notes, and snippets.

@rvause
Last active December 16, 2015 07:19
Show Gist options
  • Select an option

  • Save rvause/5397411 to your computer and use it in GitHub Desktop.

Select an option

Save rvause/5397411 to your computer and use it in GitHub Desktop.
as_json decorator for Django views
import json
from django.http import HttpResponse
def as_json(func):
def decorator(request, *ar, **kw):
output = func(request, *ar, **kw)
if not isinstance(output, dict):
return output
return HttpResponse(json.dumps(output), 'application/json')
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment