from django.http import HttpResponseRedirect
class AdminRedirectMiddleware:
""" allows you to customize redirects with the GET line in the admin """
def process_response(self, request, response):
# save redirects if given
if request.method == "GET" and request.GET.get("next", False):
request.session["next"] = request.GET.get("next")
# apply redirects
if request.session.get("next", False) and \
type(response) == HttpResponseRedirect and \
request.path.startswith("/admin/"):
path = request.session.get("next")
del request.session["next"]
return HttpResponseRedirect(path)
return response
Comments
Thanks, this has been really useful to me.
I did find one problem- URLs with no trailing slash fail with an AttributeError, cos request is a WSGIRequest and has no 'session' attr. This is nasty because it doesn't throw a 500 error.
I've wrapped it in a try block just now till I have time to look at what's going on.
#
@derek That's probably because django redirects to add the trailing slash, and the session is not available on the redirect. Catching AttributeError and returning the response is a reasonable workaround.
#