Last active
December 16, 2015 07:19
-
-
Save rvause/5397411 to your computer and use it in GitHub Desktop.
as_json decorator for Django views
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
| 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