In my views I am using some module which connects to external api.
I want this module to throw some custom exception like for example ApiServerNotRespond.
Then I want this exception to cause custom view/template to be rendered. For example if in my view “connect_to_api” (where I am using my api module) api module will cause ApiServerNotRespond exception then for example not_respond view is called instead or just myapp/not_respomd.html template rendered.
I don’t want to use any middleware things because I want all config reside in my app dir not in settings file.
Where and how I can define custom exception and how to force it to render custom template after it is raised?
I think you can achieve this by writing a decorator.
The decorator should catch
ApiServerNotRespondor any other exception you want. And if such exception occurs return response with template you want otherwise just return response from the original view.Sample:
In your views.py,