Quick tip time!
Need to debug a Django website or API that you're building, but don't want to disrupt service for other developers or clients using your app? Well then, you're in luck, because this tip is for you!
If you don't know already, the python debugger, aka pdb, is the best tool ever. Just call pdb.set_trace() in your python apps and get an interactive shell wherever you want. Awesome!
However, that doesn't work well for debugging APIs, because it will cause your web server to either time out or return 400 or 500 errors because the debugger blocks the app from returning a real HTTP response.
However, with this little snippet, you can have your cake an eat it to! It will create a new thread with your debugger, and return an HTTP response to your client. Yay!
|from django.http import HttpResponse, HttpResponseRedirect|
|from django.template import RequestContext|
|from django.shortcuts import get_object_or_404, render_to_response|
|def debug(args, **kwargs):|
|request = args['request']|
|t = threading.Thread(target=debug,|
There you go! See ways to improve this script? Feel free to fork and comment below!