Generic view 'redirect_to' that supports QUERY_STRING

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from django.http import HttpResponseGone, HttpResponsePermanentRedirect,\
    HttpResponseRedirect

def redirect_to(request, url, permanent=True, **kwargs):
    if url is not None:
        klass = permanent and HttpResponsePermanentRedirect or HttpResponseRedirect
        url = url % kwargs

        if request.GET:
            url = '?'.join([url, request.META['QUERY_STRING']])

        return klass(url)
    
    return HttpResponseGone()

More like this

  1. Persistent Params Decorator by achimnol 2 years, 6 months ago
  2. login_required decorator that doesn't redirect by brutasse 11 months, 4 weeks ago
  3. Redirect view based on GEO by jorjun 2 years, 6 months ago
  4. login_required for a generic view in URLconf by pgugged 3 years, 7 months ago
  5. django redirects middleware a bit more fleixble by robertrv 2 years, 3 months ago

Comments

mlissner (on January 27, 2012):

This is now supported in Django 1.3 out of the box.

#

(Forgotten your password?)