[Django]-Django debug error page not working on AJAX calls

2👍

The “nice debug page” depends on the DEBUG setting (from https://docs.djangoproject.com/en/dev/ref/settings/#debug)

One of the main features of debug mode is the display of detailed error pages.
If your app raises an exception when DEBUG is True, Django will display a 
detailed traceback, including a lot of metadata about your environment, such
as all the currently defined Django settings (from settings.py).

TEMPLATE_DEBUG adds extra information related to template rendering errors.

UPDATE:

That’s the way it’s supposed to work if request.is_ajax() is true then a text traceback is returned in the response body, check the source:

https://github.com/django/django/blob/master/django/views/debug.py#L59-70

This behavior was changed in 1.4, commit: https://github.com/django/django/commit/0d9b6a5bc43c06716212bd3f847460ce985381aa

UPDATE 2:

It’s kinda hackish but for debugging purposes you could modify the HTTP_X_REQUESTED_WITH header so request.is_ajax() is false, then forcing the html response. (See https://github.com/django/django/blob/master/django/http/request.py#L136-137)

👤gonz

1👍

The TEMPLATE_DEBUG option only works for errors that occur while rendering a Django template. Since your function does not render any templates, the option will have no effect.

From https://docs.djangoproject.com/en/dev/ref/settings/#template-debug:

A boolean that turns on/off template debug mode. If this is True, the
fancy error page will display a detailed report for any exception
raised during template rendering. This report contains the relevant
snippet of the template, with the appropriate line highlighted.

This also means that even if your function does render a template, if the error occurs before the template is rendered you will still end up with the plain-text output.

Note that in the following example:

def year_archive(request, year):
    a_list = Article.objects.filter(pub_date__year=year)
    raise Exception("something horrible happened")
    return render_to_response('news/year_archive.html', {'year': year, 'article_list': a_list})

The Exception is being raised before render_to_response, so TEMPLATE_DEBUG will never have a chance to run.

If you want to see the traceback for a non-template error, you will need to look at the command line output or the logs.

Leave a comment