-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added middleware for Django developer toolbar to be print JSON as HTM…
…L, which enables the toolbar for API calls. Append an empty query parameter to an API call like so: http://192.168.50.50:8000/api/v1/data_sets/?format=json&debug
- Loading branch information
Showing
2 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class JsonAsHTML(object): | ||
""" | ||
View a JSON response in your browser as HTML if the request has a `debug` | ||
query parameter | ||
E.g. http://192.168.50.50:8000/api/v1/data_sets/?format=json&debug | ||
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 request.GET.get('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>{title}</title></head><body><pre>{body}" | ||
"</pre></body></html>".format( | ||
title=title, | ||
body=response.content | ||
) | ||
) | ||
response['Content-Type'] = 'text/html' | ||
return response |
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
b57277e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are web browsers plugins like Postman that address this on the client side.
b57277e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please show us how to enable the Django debug toolbar with Postman.
b57277e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I was thinking of just the problem of formatting JSON output. Still, a better solution might be to create a simple Tastypie HTML serializer to avoid overhead of middleware which may be relevant for performance profiling.