from functools import wraps from django.http import HttpResponsePermanentRedirect, HttpResponseRedirect def permanent_redirect(url): """ Executes a HTTP 301 (permanent) redirect after the view finishes processing. If a value is returned, it is ignored. Allows for the view url to be callable so the reverse() lookup can be used. @permanent_redirect('/another-url/') def redirect_view(request): ... @redirect(lambda: reverse('some_viewname')) def do_redirect(request): ... """ def outer(f): @wraps(f) def inner(request, *args, **kwargs): f(request, *args, **kwargs) return HttpResponsePermanentRedirect(url if not callable(url) else url()) return inner return outer def redirect(url): """ Executes a HTTP 302 redirect after the view finishes processing. If a value is returned, it is ignored. Allows for the view url to be callable so the reverse() lookup can be used. @redirect('http://www.google.com/') def goto_google(request): pass @redirect(lambda: reverse('some_viewname')) def do_redirect(request): ... """ def outer(f): @wraps(f) def inner(request, *args, **kwargs): f(request, *args, **kwargs) return HttpResponseRedirect(url if not callable(url) else url()) return inner return outer