How to debug in Django, the good way?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: The World Wide Mind
--
Chapters
00:00 Question
01:19 Accepted answer (Score 597)
02:32 Answer 2 (Score 234)
03:06 Answer 3 (Score 174)
03:38 Answer 4 (Score 84)
04:46 Thank you
--
Full question
https://stackoverflow.com/questions/1118...
Question links:
[Python]: http://en.wikipedia.org/wiki/Python_%28p...
[Django]: http://en.wikipedia.org/wiki/Django_%28w...
Accepted answer links:
[Python debugger]: https://docs.python.org/2/library/pdb.ht...
[IPDB]: http://pypi.python.org/pypi/ipdb/
[ipython]: http://ipython.org/
[pdb++]: https://pypi.python.org/pypi/pdbpp/
[Antash]: https://stackoverflow.com/users/5792269/...
[pudb]: https://pypi.python.org/pypi/pudb
[PatDuJour]: https://stackoverflow.com/users/5081188/...
[Using the Python debugger in Django]: https://mike.tig.as/blog/2010/09/14/pdb/
[Seafangs]: https://stackoverflow.com/users/884640/s...
Answer 2 links:
[Werkzeug]: http://werkzeug.pocoo.org/
[django-extensions]: https://github.com/django-extensions/dja...
Answer 4 links:
[Django debug toolbar]: https://github.com/django-debug-toolbar/...
[logging]: http://docs.python.org/library/logging.h...
[firepython]: http://github.com/darwin/firepython/tree...
[firebug]: http://getfirebug.com/
[django-viewtools]: http://eric.themoritzfamily.com/2009/02/.../
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #debugging
#avk47
ACCEPTED ANSWER
Score 611
There are a bunch of ways to do it, but the most straightforward is to simply use the Python debugger. Just add following line in to a Django view function:
import pdb; pdb.set_trace()
or
breakpoint() #from Python3.7
If you try to load that page in your browser, the browser will hang and you get a prompt to carry on debugging on actual executing code.
However there are other options (I am not recommending them):
* return HttpResponse({variable to inspect})
* print {variable to inspect}
* raise Exception({variable to inspect})
But the Python Debugger (pdb) is highly recommended for all types of Python code. If you are already into pdb, you'd also want to have a look at IPDB that uses ipython for debugging.
Some more useful extension to pdb are
Using the Python debugger in Django, suggested by Seafangs.
ANSWER 2
Score 236
I really like Werkzeug's interactive debugger. It's similar to Django's debug page, except that you get an interactive shell on every level of the traceback. If you use the django-extensions, you get a runserver_plus managment command which starts the development server and gives you Werkzeug's debugger on exceptions.
Of course, you should only run this locally, as it gives anyone with a browser the rights to execute arbitrary python code in the context of the server.
ANSWER 3
Score 174
A little quickie for template tags:
@register.filter
def pdb(element):
import pdb; pdb.set_trace()
return element
Now, inside a template you can do {{ template_var|pdb }} and enter a pdb session (given you're running the local devel server) where you can inspect element to your heart's content.
It's a very nice way to see what's happened to your object when it arrives at the template.
ANSWER 4
Score 86
There are a few tools that cooperate well and can make your debugging task easier.
Most important is the Django debug toolbar.
Then you need good logging using the Python logging facility. You can send logging output to a log file, but an easier option is sending log output to firepython. To use this you need to use the Firefox browser with the firebug extension. Firepython includes a firebug plugin that will display any server-side logging in a Firebug tab.
Firebug itself is also critical for debugging the Javascript side of any app you develop. (Assuming you have some JS code of course).
I also liked django-viewtools for debugging views interactively using pdb, but I don't use it that much.
There are more useful tools like dozer for tracking down memory leaks (there are also other good suggestions given in answers here on SO for memory tracking).