simao simao - 6 months ago 27
Python Question

How to obtain a plain text Django error page

During development, I am running Django in Debug mode and I am posting data to my application using a text mode application. Ideally, I need to receive a plain text response when I get an http error code 500 so I don't have to look for the real error inside all that HTML and Javascript.

Is it possible to obtain a Django 500 Internal Server Error as plain text?

Answer

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
Comments