Pregunta

I would like to block or redirect all requests to another service temporarily in django request/response module. However, I do want to do this put a control mechanism at the begining of all service functions. For example there is a sginal request_start which is sent when a request incomes to any restful API. In handler, is it possible to also block these requests or stop django temporarily?

¿Fue útil?

Solución

If you want to simply filter requests by context (for example by the URL) or reject all of them, then you can write your own middleware with a process_request method, where you can check for condition(s) and return either None (to continue processing) or HttpResponse with redirect/404/403 (to block processing).

Now, if you want to send signals or perform any other processing, you can do it in another middleware and simply set a proper order in MIDDLEWARE_CLASSES (the "blocking" middleware should be the last).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top