Two Django debugging methods

Source: Internet
Author: User

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/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.