How often do you use print
statements to display output in your manage.py
runserver
process to help debug something?
This was my go-to method of debugging for a long time.
Then I was introduced to pdb.
This is so much better and useful, especially when you have situations where you have to do a lot of things in your web application in order to reproduce the conditions you are trying to debug.
Using pdb and the interactive debugger is as simple as putting:
import pdb; pdb.set_trace()
on the line that you want to start the debug session on. When you do this and
you hit that line in the execution of your application, you’ll see a prompt
that looks like this in your manage.py runserver
output:
> /Users/paltman/dev/mywebsite/mywebsite/profiles/views.py(29)profiles_list()
-> if request.user.is_superuser:
(Pdb)
From here you can issue single letter commands like n
, c
, or s
, which
stand or Next, Step Into, or Continue.
Next (n
) will simply take you to the next line of execution within the
current context. Step Into (s
) will jump a level deeper (if it makes sense),
for example it will step into a function call being made instead of just
executing it and returning the results. Finally, Continue (c
) will release
the debugger and continue executing until a response is sent back to the
browser or it encounters another breakpoint.
There is some great documentation on this feature which I recommend reading further if interested in this.