Reputation: 951
I have some custom middleware that creates a stack of previous URLS so that they can be navigated back to from subsequent pages.
In one of my views that adds its url to the stack, there is some logic that can redirect the response to a different page, so:
@middleware_decorator # tells the middleware to add this views url to the stack when it is called
def some_view(request):
... stuff ...
if some_condition:
return HttpResponseRedirect(url, kwargs)
The issue I have is, if I hit the response redirect condition, I don't want to add the current url to the stack, because when they go back from the next page, they will just hit the same condition again and be redirected to the page they just left. I have a "remove" function in the middleware but I can't call it like so:
if some_condition:
Middleware.remove("this views url")
return HttpResponseRedirect(url, kwargs)
Because the view url is added in the process_response stage of the middleware for logic reasons so occurs after the redirect has taken place. I was hoping there is some way detecting in the middleware after the first view has been redirected from- "A redirect has just happened" and then in the middleware I can decide whether to add it or not.
Upvotes: 1
Views: 937
Reputation: 31643
You can set a flag in the request if it should be added to the stack and handle the actual adding to functioanality in process_response
process_response(self, request, response):
if request.addToStack and not instanceof(response, HttpResponseRedirect):
#Add url from request to stack
Upvotes: 4