[Django] #30038: New shortcut: redirect_with_params()

6 views
Skip to first unread message

Django

unread,
Dec 12, 2018, 1:43:27 PM12/12/18
to django-...@googlegroups.com
#30038: New shortcut: redirect_with_params()
---------------------------------------------+------------------------
Reporter: agustin bacigalup | Owner: nobody
Type: New feature | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
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)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30038>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 12, 2018, 1:50:07 PM12/12/18
to django-...@googlegroups.com
#30038: New shortcut: redirect_with_params()
-----------------------------------+--------------------------------------

Reporter: agustin bacigalup | Owner: nobody
Type: New feature | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Description changed by agustin bacigalup:

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>

Django

unread,
Dec 17, 2018, 5:43:38 AM12/17/18
to django-...@googlegroups.com
#30038: New shortcut: redirect_with_params()
-----------------------------------+--------------------------------------
Reporter: agustin bacigalup | Owner: nobody
Type: New feature | Status: closed
Component: Uncategorized | Version: 2.1
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* 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>

Reply all
Reply to author
Forward
0 new messages