How to obtain a plain text Django error page

I think to write a middleware, because otherwise the exception isn't available in the 500.html

http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception

class ProcessExceptionMiddleware(object):
    def process_exception(self, request, exception):
        t = Template("500 Error: {{ exception }}")
        response_html = t.render(Context({'exception' : exception }))

        response = http.HttpResponse(response_html)
        response.status_code = 500
        return response

If you are looking for a way to get a plain text error page when using curl, you need to add the HTTP header X-Requested-With with value XMLHttpRequest, e.g.

curl -H 'X-Requested-With: XMLHttpRequest' http://example.com/some/url/

Explanation: this is because Django uses the is_ajax method to determine whether or not to return as plain text or as HTML. is_ajax in turn looks at X-Requested-With.


Update

Since django version 3.1, error reporting ignores the X-Requested-With header. Instead, set the Accept header on the request to any valid value which does not include the text/html mime type.

e.g.

curl -H 'Accept: application/json;charset=utf-8' http://example.comp/some/url

There's a setting DEBUG_PROPAGATE_EXCEPTIONS which will force Django not to wrap the exceptions, so you can see them, e.g. in devserver logs.