How can I get Django to print the debug information to the console?

后端 未结 6 1822
故里飘歌
故里飘歌 2020-12-04 19:57

I\'m using urlib to hit my app not a browser so I can\'t see the debug screen when an error occurs. What\'s the best way to send the normal debug info to the console or a fi

6条回答
  •  鱼传尺愫
    2020-12-04 20:24

    I was getting 500 Internal Server Error and was not able to debug it as the code was not accessed through a browser and terminal didn't show the traceback. This bit of code in a middleware made all the difference:

    import traceback
    import sys
    
    class ProcessExceptionMiddleware(object):
        def process_response(self, request, response):
            if response.status_code != 200:                
                print '\n'.join(traceback.format_exception(*sys.exc_info()))
            return response
    

    Instead of processing the exception I, am processing the response object and printing the traceback when the status code not 200 and voila I can now see what is causing the error. Which in this particular case was an improperly indented block.

    Yuji's answer above and the middleware documentation helped me reach this solution.

提交回复
热议问题