WebProgramDebugging is very different from the desktop program, and debugging is a problem for Django programs. Although there are third-party debugging tools, robhudson-Django-debug-toolbar, however, it is not very convenient to use, and many people are used to debugging through print, which is convenient and quick. The following describes two simple debugging methods:
1. Make full use of Django's error page
Django's error page is powerful and provides detailed traceback, including the value of local variables and plain text exception information. It has the same effect as phpinfo () and can display the settings of the current application, including get, post and cookie data in the request and all important meta fields in the HTTP environment.
You can use
AssertFalse
AssertFalse, request. Get
To trigger Django error pages, and then perform debugging.
2. output the log to the Development Server Terminal.
Using Python's logging Module
Add the following configuration in setting. py:
ImportLogging
Logging. basicconfig (
Level=Logging. debug,
Format= '% (Asctime) S % (levelname) S % (Message) S',
)
You can call this method where you need to output log information.
ImportLogging
Logging. debug ("A log message")
In this way, you can see the log to be output on the terminal of the development server. If you want to record the log information to the specified file, adjust the basicconfig of logging, as shown below:
Logging. basicconfig (
Level = Logging. debug,
Format = ' % (Asctime) S % (levelname) S % (Message) S ' ,
Filename = ' /Tmp/MyApp. Log ' ,
Filemode = ' W '
)
Sometimes we find that our program runs normally in most cases and only has errors in a specific environment. In this case, we can use the traceback module to record the current stack storage information.To facilitate debugging. The calling method is as follows:
ImportLogging, traceback, pprint
DefMy_buggy_function (ARG ):
...
IfError_condition:
Stack=Pprint. pformat (traceback. extract_stack ())
Logging. debug ('An error occurred: % s' %Stack)
The above debugging method comes from the network, see: http://simonwillison.net/2008/May/22/debugging/