56

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
  • Small typo in the answer from Manoj Govindan: params = urllib.urlencode(**kwargs) needs to be replaced with params = urllib.urlencode(kwargs) Otherwise you get "TypeError: urlencode() got an unexpected keyword argument"
    – Max Arnold
    Jun 13, 2011 at 9:19

4 Answers 4

129

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

response = redirect('url-name', x)
response['Location'] += '?your=querystring'
return response
3
  • Easy enough to use urldefrag if that's a possibility. Thanks for pointing out the potential error if you have an anchor in your url response. Dec 1, 2016 at 22:20
  • Best answer ever Apr 13, 2017 at 16:57
  • @Kolyunya: Are you sure? I'm using it with an anchor May 20, 2019 at 18:39
48

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'
1
  • It's crazy this is not part of Django framework... Any philosophical reason behind?
    – Alex Gidan
    Apr 13, 2017 at 11:27
4

We can import urlencode from django.

from django.utils.http import urlencode

get_args_str = urlencode({'q': 'something'})

Or we can just use unparsed get parameters string from starting request

get_args_str = request.META['QUERY_STRING']

HttpResponseRedirect('%s?%s' % (url, get_args_str))
2
  • 1
    I think the x should be removed to make this work. Otherwise the contents of x are added as a body in the HTTP response when it should be empty. Apr 18, 2019 at 17:58
  • Yes. Thank you for finding this bug. Edited. Apr 18, 2019 at 19:24
2

I think it's worth noting that Django's RedirectView class has a built-in class attribute query_string which can be overridden or passed to as_view. If set to True, query_string will preserve the query string in the redirect. For example, you could put the following in urls.py:

path('example-redirect/',
     RedirectView.as_view(url='https://example.com', query_string=True),
     name='example_redirect')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.