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 pdb module that can temporarily be 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:

import pdb

pdb.set_trace()

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 commands of the Python debugger 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 the 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.
..................Content has been hidden....................

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