Created
March 17, 2014 01:09
-
-
Save Benoss/9592229 to your computer and use it in GitHub Desktop.
Django add Debug Toolbar for non HTML response ex: Json rest response
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
DEBUG=True | |
SOUTH_TESTS_MIGRATE = True | |
DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. | |
'NAME': 'geo', # Or path to database file if using sqlite3. | |
'USER': 'postgres', # Not used with sqlite3. | |
'PASSWORD': 'postgres', # Not used with sqlite3. | |
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3. | |
'PORT': '', # Set to empty string for default. Not used with sqlite3. | |
} | |
} | |
DEBUG_INSTALLED_APPS = ( | |
'debug_toolbar', | |
) | |
from django.http import HttpResponse | |
import json | |
DEBUG_MIDDLEWARE_CLASSES = ( | |
#add DebugToolbarMiddleware and NonHtmlDebugMiddleware | |
'debug_toolbar.middleware.DebugToolbarMiddleware', | |
'geoservice.local_settings.NonHtmlDebugToolbarMiddleware', | |
) | |
class NonHtmlDebugToolbarMiddleware(object): | |
""" | |
The Django Debug Toolbar usually only works for views that return HTML. | |
This middleware wraps any non-HTML response in HTML if the request | |
has a 'debug' query parameter (e.g. http://localhost/foo?debug) | |
Special handling for json (pretty printing) and | |
binary data (only show data length) | |
""" | |
@staticmethod | |
def process_response(request, response): | |
debug = request.GET.get('debug', 'UNSET') | |
if debug != 'UNSET': | |
if response['Content-Type'] == 'application/octet-stream': | |
new_content = '<html><body>Binary Data, ' \ | |
'Length: {}</body></html>'.format(len(response.content)) | |
response = HttpResponse(new_content) | |
elif response['Content-Type'] != 'text/html': | |
content = response.content | |
try: | |
json_ = json.loads(content) | |
content = json.dumps(json_, sort_keys=True, indent=2) | |
except ValueError: | |
pass | |
response = HttpResponse('<html><body><pre>{}' | |
'</pre></body></html>'.format(content)) | |
return response |
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
#This is the end of settings.py | |
try: | |
from local_settings import * | |
try: | |
MIDDLEWARE_CLASSES += DEBUG_MIDDLEWARE_CLASSES | |
except NameError: | |
pass | |
try: | |
INSTALLED_APPS += DEBUG_INSTALLED_APPS | |
except NameError: | |
pass | |
except ImportError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Django 1.10+ use the MiddlewareMixin and modify the process_response function to be an instance method rather than a static method: