Django: Fix a view using a debugger with breakpoint()
adamj.eu
adamj.eu
import signal
def sigusr1_handler(signum, stack):
breakpoint()
signal.signal(signal.SIGUSR1, sigusr1_handler)
And then I can drop into pdb by sending a signal: $ kill -s sigusr1 $PID
It's especially useful for getting a backtrace out of your program when it's hanging, and poking around a bit to figure out why.I was afraid it died as it didn’t get updated for python 3.12, but that’s thankfully apparently been solved now!
@register.simple_tag(takes_context=True)
def set_breakpoint(context, *args):
vars = [arg for arg in locals()["args"]]
breakpoint()
Then add the breakpoint in your template wherever you like with {% set_breakpoint %}- you will probably forget to clean up after the fact leaving you with both performance and security problems
- you should never debug in production, use something like Sentry instead also, better to replicate the production environment in a sandbox (either on your machine using Vagrant or Docker, etc. if feasible) or in the cloud by taking a snapshot of your production environment and using it as a sandbox to debug (and then of course destroying it after use).