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. login_required for a generic view in URLconf by pgugged 5 years, 9 months ago
  2. Generic Views for newforms by jnievas 6 years, 2 months ago
  3. ajax form handler generic view by amitu 5 years, 5 months ago
  4. Persistent Params Decorator by achimnol 4 years, 8 months ago
  5. View response's content in a browser while testing by ryankask1 3 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?)