Django 1.4 AJAX debug style error

In Django 1.4, whenever I wanted to show server errors when making AJAX requests, I couldn’t see the formatted error page, only the response text. This is something new in Django 1.4, because previous versions didn’t have this problem.

The fix is simple, but has to be done directly on the framework code, so be careful.

Open the file django/views/debug.py, find the function technical_500_response and comment the part that handles ajax requests, since it returns the error code as simple text, instead of returning it as html. Mind the indentation:


def technical_500_response(request, exc_type, exc_value, tb):
"""
Create a technical server error response. The last three arguments are
the values returned from sys.exc_info() and friends.
"""
reporter = ExceptionReporter(request, exc_type, exc_value, tb)
"""
if request.is_ajax():
text = reporter.get_traceback_text()
return HttpResponseServerError(text, mimetype='text/plain')
else:
"""
html = reporter.get_traceback_html()
return HttpResponseServerError(html, mimetype='text/html')

Sources:

https://code.djangoproject.com/ticket/18229
https://github.com/django/django/commit/0d9b6a5bc4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: