Skip to content

Instantly share code, notes, and snippets.

@peterbe
Created June 27, 2014 22:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterbe/16c05571f5c495c83505 to your computer and use it in GitHub Desktop.
Save peterbe/16c05571f5c495c83505 to your computer and use it in GitHub Desktop.
"""
To use this, add::
MIDDLEWARE_CLASSES += ('airmozilla.base.middleware.JsonAsHTML',)
to your settings and for any AJAX request, you can open it
separately and add `&_debug=true` to the URL.
"""
from urllib import quote
from django.conf import settings
class JsonAsHTML(object): # pragma: no cover
'''
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() or not request.GET.get('_debug'):
return response
# only do something if this is a json response
if response['Content-Type'].lower().startswith('application/json'):
title = (
'JSON as HTML Middleware for: %s' %
quote(request.get_full_path())
)
content = response.content
content = content.replace('<', '&lt;').replace('>', '&gt;')
response.content = (
'<!doctype html>\n<html><head><title>%s</title>'
'<meta content="text/html; charset=UTF-8" '
'http-equiv="Content-Type">'
'</head>\n<body>\n%s\n</body></html>'
% (title, 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