Skip to content

Instantly share code, notes, and snippets.

@chriskief
Created November 20, 2013 17:14
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/7567071 to your computer and use it in GitHub Desktop.
Save chriskief/7567071 to your computer and use it in GitHub Desktop.
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