Last active
September 20, 2017 23:42
-
-
Save marteinn/5693665 to your computer and use it in GitHub Desktop.
Example on how to debug Tastypie using Django Debug Toolbar
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
def html_decorator(func): | |
""" | |
This decorator wraps the output in html. | |
(From http://stackoverflow.com/a/14647943) | |
""" | |
def _decorated(*args, **kwargs): | |
response = func(*args, **kwargs) | |
wrapped = ("<html><body>", | |
response.content, | |
"</body></html>") | |
return HttpResponse(wrapped) | |
return _decorated | |
@html_decorator | |
def debug(request): | |
""" | |
Debug endpoint that uses the html_decorator, | |
""" | |
path = request.META.get("PATH_INFO") | |
api_url = path.replace("debug/", "") | |
view = urlresolvers.resolve(api_url) | |
accept = request.META.get("HTTP_ACCEPT") | |
accept += ",application/json" | |
request.META["HTTP_ACCEPT"] = accept | |
res = view.func(request, **view.kwargs) | |
return HttpResponse(res._container) | |
# And finally attach debug to urls (if settings is on) | |
if settings.DEBUG: | |
urlpatterns += patterns('', | |
url(r'^debug/', debug), | |
) | |
""" | |
Usage: | |
Lets say your original endpoint is this: | |
http://example.com/api/v1/cities/ | |
You use the following url the debug it | |
http://example.com/api/debug/v1/cities/ | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@marteinn: just what I was looking for, thanks!