Search code examples
djangodjango-templatesrendermiddleware

Is it possible to render a template from middleware?


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?


Solution

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