Last active
April 1, 2016 11:25
-
-
Save lucianmarin/bda94b3b12edeb18a90d to your computer and use it in GitHub Desktop.
/?debug DRF endpoints
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
# 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