django - Detecting a response redirect in middleware -
i have custom middleware creates stack of previous urls can navigated subsequent pages.
in 1 of views adds url stack, there logic can redirect response different page, so:
@middleware_decorator # tells middleware add views url stack when called def some_view(request): ... stuff ... if some_condition: return httpresponseredirect(url, kwargs)
the issue have is, if hit response redirect condition, don't want add current url stack, because when go next page, hit same condition again , redirected page left. have "remove" function in middleware can't call so:
if some_condition: middleware.remove("this views url") return httpresponseredirect(url, kwargs)
because view url added in process_response stage of middleware logic reasons occurs after redirect has taken place. hoping there way detecting in middleware after first view has been redirected from- "a redirect has happened" , in middleware can decide whether add or not.
you can set flag in request if should added stack , handle actual adding functioanality in process_response
process_response(self, request, response): if request.addtostack , not instanceof(response, httpresponseredirect): #add url request stack