views:

66

answers:

2

Is possible to add GET variables in a redirect ? (Without having to modifiy my urls.py)

If I do redirect('url-name', x)

I get HttpResponseRedirect('/my_long_url/%s/', x)

I don't have complains using HttpResponseRedirect('/my_long_url/%s/?q=something', x) instead, but just wondering...

+1  A: 

Is possible to add GET variables in a redirect ? (Without having to modifiy my urls.py)

I don't know of any way to do this without modifying the urls.py.

I don't have complains using HttpResponseRedirect('/my_long_url/%s/?q=something', x) instead, but just wondering...

You might want to write a thin wrapper to make this easier. Say, custom_redirect

def custom_redirect(url_name, *args, **kwargs):
    from django.core.urlresolvers import reverse 
    import urllib
    url = reverse(url_name, args = args)
    params = urllib.urlencode(**kwargs)
    return HttpResponseRedirect(url + "?%s" % params)

This can then be called from your views. For e.g.

return custom_redirect('url-name', x, q = 'something')
# Should redirect to '/my_long_url/x/?q=something'
Manoj Govindan
+1 Elegant solution, thanks.
juanefren
+1  A: 

Since redirect just returns an HttpResponseRedirect object, you could just alter that:

response = redirect('url-name', x)
response['Location'] += '?your=querystring'
return response
SmileyChris
+1 Fast solution that works, thanks.
juanefren