One of the great features of Django is that when in debugging mode something goes wrong with your code, you will see the traceback of the error and many other useful information directly in your browsers window.
This is cool and helps you to quickly debug your code. The problem comes when you start to put Ajax into your site. Then the result of a request is not directly displayed in the window and all you can see is the HTTP return code of 500 in the log of your server.
In this short post I will show a simple yet useful solution to this problem. The goal is to get the error messages somehow. In my case, it is sufficient to read it from the log of the development server. To accomplish this, I wrote a simple decorator (wrapper) that you can use in your view function (or anywhere else if you wish). Like this:
The code for the decorator, which I choose to place in a separate module, but you can also place it somewhere in views.py, is as follows:
def x(*args, **kw):
ret = f(*args, **kw)
except Exception, e:
print >> sys.stderr, "ERROR:", str(e)
exc_type, exc_value, tb = sys.exc_info()
message = "Type: %s\nValue: %s\nTraceback:\n\n%s" % (exc_type, exc_value, "\n".join(traceback.format_tb(tb)))
print >> sys.stderr, message