Custom Admin Redirects

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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

More like this

  1. Django Admin Replacer Code by riccardodivirgilio 3 years, 5 months ago
  2. a template tag to invoke a method on an object with a variable by Scanner 6 years, 11 months ago
  3. keeping filter states after edits by chronosllc 2 years, 8 months ago
  4. Use dateutil's relativedelta to sort an arbitrary list of Django objects by date by cmcavoy 7 years, 1 month ago
  5. Find deletable objects by writefaruq 2 years, 10 months ago

Comments

derek (on May 5, 2009):

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.

#

bigfudge (on July 5, 2012):

@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.

#

(Forgotten your password?)