There's more...

Before creating a monkey patch, we need to completely understand how the code that we want to modify works. This can be done by analyzing the existing code and inspecting the values of different variables. To do this, there is a useful built-in Python debugger module, pdb, that can be temporarily added to the Django code (or any third-party module) to stop the execution of a development server at any breakpoint. Use the following code to debug an unclear part of a Python module:

breakpoint()

This launches the interactive shell, where you can type in the variables in order to see their values. If you type c or continue, the code execution will continue until the next breakpoint. If you type q or quit, the management command will be aborted.

You can learn more Python debugger commands and how to inspect the traceback of the code at https://docs.python.org/3/library/pdb.html.

Another quick way to see the value of a variable in the development server is to raise a warning with the variable as a message, as follows:

raise Warning, some_variable

When you are in DEBUG mode, the Django logger will provide you with the traceback and other local variables.

Don't forget to remove debugging code before committing your work to a repository.

If you are using the PyCharm interactive development environment, you can set breakpoints and debug variables there visually without modifying the source code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.144.32