{{{
def redirect_with_params(request, to, *args, **kwargs):
"""
Same as `django.shortcuts.redirect` but recieves a `request` as first
parameter.
"""
response = redirect(to, *args, **kwargs)
url = response.url
params = request.GET.urlencode()
if params:
url = "%s?%s" % (response.url, params)
return response.__class__(url)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30038>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> I'd like to contribute to Django by adding a shorcut, similar to
> `django.shortcuts.redirect` but also appends request's GET parameters. I
> found myself using it a lot, and I wonder if it's useful for the
> community. If so, should I fork Django on Github and submit a pull
> request?
>
> {{{
> def redirect_with_params(request, to, *args, **kwargs):
> """
> Same as `django.shortcuts.redirect` but recieves a `request` as first
> parameter.
> """
> response = redirect(to, *args, **kwargs)
> url = response.url
> params = request.GET.urlencode()
> if params:
> url = "%s?%s" % (response.url, params)
> return response.__class__(url)
> }}}
New description:
I'd like to contribute to Django by adding a shorcut, similar to
`django.shortcuts.redirect` but also appends request's GET parameters. I
found myself using it a lot, and I wonder if it's useful for the
community. If so, should I fork Django on Github and submit a pull
request?
{{{
def redirect_with_params(request, to, *args, **kwargs):
"""
Same as `django.shortcuts.redirect` but recieves a `request` as first
parameter.
It preserves the GET parameters from the `request` in the URL
redirected.
"""
response = redirect(to, *args, **kwargs)
url = response.url
params = request.GET.urlencode()
if params:
url = "%s?%s" % (response.url, params)
return response.__class__(url)
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/30038#comment:1>
* status: new => closed
* resolution: => wontfix
Comment:
Hi Augustin,
If you look at `RedirectView`, you'll see the `query_string` attribute. It
might be good to mirror the variable name there.
[https://github.com/django/django/blob/1939dd49d142b65fa22eb5f85cee0d20864d3730/django/views/generic/base.py#L173-L175
It also uses `request.META`'s `'QUERY_STRING`'], which might save you a
step:
{{{
args = self.request.META.get('QUERY_STRING', '')
if args and self.query_string:
url = "%s?%s" % (url, args)
}}}
I can see how this might be a handy addition to a project. I'm not sure it
merits an additional function in `shortcuts`. (It doesn't seem in the same
order of utility as the existing methods. `redirect()` itself is 9 or 10
years old, so we've been living without this for a while...) I guess for
me it's the sort of thing that can happily live in a blog post or a gist
or a third-party package (if it were bundled up with other things).
Happy if you want to ask if there's appetite to include it on django-
developers, but at this point I'll lean towards not. Thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/30038#comment:2>