views:

46

answers:

3

I have a middleware that does some processing. On certain conditions it raises an exception and the user sees my 500.html template - correctly responding to 500 http status.

Now, on some exceptions I would like to render different template than default 500.html. Is it possible/how to achieve that?

A: 

Yes... and no.

You can render whatever you want (your web server has a nice explanation how to do that), but whether the user will see that is his choice - through his browser settings. It is possible you render something, but the browser still shows a standard error page.

TomTom
A: 

A middleware might be a solution:

class MyExceptionMiddleware:
   def process_exception(self, request, exception):

     if isinstance(exception, CustomException):
       template = loader.get_template('Other500.html')
       context = RequestContext(request, {'message': 'Custom Message'})
       return HttpResponseForbidden(template.render(context))

     return None

Don't forget to register the middleware in settings.py:

MIDDLEWARE_CLASSES = (
    ....
    'app.middleware.MyExceptionMiddleware',
maersu
+1  A: 

You can catch those exceptions and return a HttpResponse object to render your custom template. Or maybe a redirect is also appropriate.

aeby