Login

Generic view 'redirect_to' that supports QUERY_STRING

Author:
marinho
Posted:
October 6, 2009
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

This generic view does the same that 'django.views.generic.simple.redirect_to' does but supports request.GET parameters.

 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. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

mlissner (on January 27, 2012):

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

#

Please login first before commenting.