Created
November 20, 2013 17:14
-
-
Save chriskief/7567071 to your computer and use it in GitHub Desktop.
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 characters
from django.conf import settings | |
class JsonAsHTML(object): | |
''' | |
View a JSON response in your browser as HTML | |
Useful for viewing stats using Django Debug Toolbar | |
This middleware should be place AFTER Django Debug Toolbar middleware | |
''' | |
def process_response(self, request, response): | |
# not for production or production like environment | |
if not settings.DEBUG: | |
return response | |
# do nothing for actual ajax requests | |
if request.is_ajax(): | |
return response | |
# only do something if this is a json response | |
if 'application/json' in response['Content-Type'].lower(): | |
title = 'JSON as HTML Middleware for: %s' % request.get_full_path() | |
response.content = '<html><head><title>%s</title></head><body>%s</body></html>' % (title, response.content) | |
response['Content-Type'] = 'text/html' | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment