So like most dummies, when I needed debug my django application I was peppering “print” statements throughout my code. It was very helpful to find out what was going on, but a pain to remember where I had put all my print statements. This was especially true when I was trying to debug classes within other classes.
Then I found the python logging module. You get the same output of the print command, but logging allows you to decide when to output the information.
In your django settings file add the lines:
import logging
if DEBUG:
logging.basicConfig(level=logging.DEBUG)
The above code is importing the logging module and setting the logging level to debug if the DEBUG setting is set to ‘True’.
Now to use in your code, just ‘import logging’ and call logging for the output you want to trace.
Super simple example:
import logging
def test_it(request):
if request.method == ‘POST’:
postDict = request.POST.copy()
logging.debug(‘From your post %s’, postDict)
return postDict
You must log in to post a comment.