Hacker News new | past | comments | ask | show | jobs | submit login
Django: Fix a view using a debugger with breakpoint() (adamj.eu)
16 points by tanelpoder 62 days ago | hide | past | favorite | 14 comments



I've shared this on here before, but another trick that I like with breakpoint() is to add this function to my Python projects so that I can drop into pdb by sending a signal:

    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.


It doesn’t give you a debug repl, but my favourite tool for this sort of thing is py-spy; small and easy enough to install into a stuck pod’s ephemeral storage and and it can dump the stacktrace or give you a top-equivalent view on what your python process is doing.

I was afraid it died as it didn’t get updated for python 3.12, but that’s thankfully apparently been solved now!


Nice trick!


Also useful is adding a breakpoint in a template using a custom template tag.

    @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 %}


My coworker used to add `assert False`s into the view so that, when the stacktrace info page comes up (Django still has these, right?), you would have access to all the locals. It's been a while since I've used Django for a project, and I am still on-the-fence about such usage lol. But it worked.


Not trolling, but what's wrong with visual remote debugging using either Pycharm or Visual Studio? I find it much more rich information wise than using pdb.


Can you set breakpoints within templates using PyCharm?


Yes, you can. It has to be where there is templating language of course (you cannot put a breakpoint on a arbitrary div for example), you can also debug the rendering of the template through Pycharm if you want to in order to have a full stack debug, but I'm used to doing that using the browser developer tools.


Would be nice if there was a way to debug a view remotely in production.


Well, as previously stated in Pycharm and Visual Studio (not tested on others) there is. If I remember correctly, both will install their requirements remotely and then allow you to do so. However, I would not recommend it:

- 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).


Is there a proper way to do this in a docker container ?


You can start the container with docker run -it so it has a tty


another option from the sibling comment is to use `remote-pdb` which you can then telnet into from outside the container after exposing the port


oh nice




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: