Skip to content

Instantly share code, notes, and snippets.

@lucianmarin
Last active April 1, 2016 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lucianmarin/bda94b3b12edeb18a90d to your computer and use it in GitHub Desktop.
Save lucianmarin/bda94b3b12edeb18a90d to your computer and use it in GitHub Desktop.
/?debug DRF endpoints
# pip install django-debug-toolbar
# /api/creatives/?format=json&debug
from django.http import HttpResponse
import json
INSTALLED_APPS += ('debug_toolbar',)
MIDDLEWARE_CLASSES += (
'debug_toolbar.middleware.DebugToolbarMiddleware',
'vhosts.platform.settings.NonHtmlDebugToolbarMiddleware',
)
class NonHtmlDebugToolbarMiddleware(object):
@staticmethod
def process_response(request, response):
if request.GET.get('debug') == '':
if response['Content-Type'] == 'application/octet-stream':
html = '<html><body>Binary Data, Length: {}</body></html>'
new_content = 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
html = '<html><body><pre>{}</pre></body></html>'
response = HttpResponse(html.format(content))
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment