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

后端 未结 6 1826
故里飘歌
故里飘歌 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:21

    Ok guys, I think this an important thing to note, as it's year 2016 and many people are using Django Rest Framework instead of standard django forms, I've wasted 2 hours on figuring out why my exceptions are being handled by a renderer and it never gets to the middleware, the solution to the problem is simple.

    http://www.django-rest-framework.org/api-guide/exceptions/#custom-exception-handling

    So like this:

    import traceback
    
    from rest_framework.views import exception_handler
    
    def custom_exception_handler(exc, context):
        # Call REST framework's default exception handler first,
        # to get the standard error response.
        response = exception_handler(exc, context)
        traceback.print_exc()
        return response
    

    And then in settings define the exception handler as such:

    REST_FRAMEWORK = {
        'EXCEPTION_HANDLER': 'utils.custom_exception_handler.custom_exception_handler'
    }
    

提交回复
热议问题