How to reverse() to url including GET parameters? Example:
.../myview?foo=bar
http://stackoverflow.com/questions/9585491/how-do-i-pass-get-parameters-
using-django-urlresolvers-reverse
http://stackoverflow.com/a/27641445/633961
It would be very nice if django could implement a short-cut which provides
this.
It would be useful for python code and template, too.
--
Ticket URL: <https://code.djangoproject.com/ticket/25582>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* version: 1.8 => master
* needs_docs: => 0
* stage: Unreviewed => Accepted
Comment:
I'm not positive this is something which should live in Django, but if
not, we could probably at least add some guidance to the docs about how to
go about it. If we do add it, it likely needs a discussion on the
DevelopersMailingList to figure out what the API should look like. See
also #10941 which asks for a template tag for creating query strings in
templates.
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:1>
Comment (by DheerendraRathor):
Changing anything in `{% url %}` might create a mess. IMHO creating a new
tag which accept a dictionary or list of tuples will practically serve the
purpose.
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:2>
Comment (by spookylukey):
As per my comment on #10941:
I think this whole area is solved by
1) For Python code: urlobject https://github.com/zacharyvoase/urlobject/
and furl https://github.com/gruns/furl
2) For template code: django-spurl - https://github.com/j4mie/django-
spurl
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:3>
Comment (by guettli):
@spookylukey for me this ticket is solved by this:
{{{
def reverse(*args, **kwargs):
get = kwargs.pop('get', {})
url = orig_reverse(*args, **kwargs)
if get:
url += '?' + urllib.urlencode(get)
return url
}}}
It is not difficult. The question is if it will be handy in django core.
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:4>
Comment (by guettli):
Unfortunatley urlencode(get) fails if the dictionary get contains unicode.
Related: http://stackoverflow.com/questions/6480723/urllib-urlencode-
doesnt-like-unicode-values-how-about-this-workaround
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:5>
Comment (by guettli):
Replying to [comment:3 spookylukey]:
> As per my comment on #10941:
>
> I think this whole area is solved by
>
> 1) For Python code: urlobject
https://github.com/zacharyvoase/urlobject/ and furl
https://github.com/gruns/furl
> 2) For template code: django-spurl - https://github.com/j4mie/django-
spurl
I would like to see a solution in django core. I guess the django core
developers don't want to pull in a dependency of one of the above
packages.
If urlencode() would support unicode, the fix would be trival. But even
with the current urlencode() of Python 2.7 the issue can be solved in
django core easily.
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:6>
Comment (by guettli):
Replying to [comment:4 guettli]:
> @spookylukey for me this ticket is solved by this:
>
> {{{
> def reverse(*args, **kwargs):
> get = kwargs.pop('get', {})
> url = orig_reverse(*args, **kwargs)
> if get:
> url += '?' + urllib.urlencode(get)
> return url
> }}}
>
> It is not difficult. The question is if it will be handy in django core.
>
Unfortunatley urlencode(get) fails if the dictionary get contains unicode.
Related: http://stackoverflow.com/questions/6480723/urllib-urlencode-
doesnt-like-unicode-values-how-about-this-workaround
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:7>
Comment (by timgraham):
The [https://groups.google.com/d/topic/django-
developers/_ZuTpMEATiM/discussion discussion on django-developers] hasn't
yielded a consensus on what the solution should look like.
I closed #26276 as a duplicate -- it suggests adding `query` and
`fragment` parameters to `reverse()`.
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:8>
Comment (by Sina):
Any update on this guys? this is now more than 2 years!!!
Why a MUST have simple feature like this should take two years to
implement?
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:9>
Comment (by Aymeric Augustin):
The reason is explained in the comment just above yours.
Please consider that you're expressing your frustration in a way that's
unlikely to motivate volunteers to work for free for you.
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:10>
* cc: Ning (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:11>
Comment (by xtsimpouris):
It feels more like this is missing from core rather than an enhancement. A
URL can be with a query string or without a query string, the same goes
also for fragment and in any case all URLs are assumed to be valid. Having
this in mind, it makes sense that `url` tag should handle all the above
cases by default. That way, it would provide a robust and unified solution
for URL creation, maybe with auto escape features. Now, by trying to write
another tag and/or searching for a solution around the internet feels like
''django can't handle creating valid URLs''?
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:12>
* cc: Herbert Fortes (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:13>
* cc: Will Gordon (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:14>
* owner: nobody => Daniel Rios
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:15>
* cc: thenewguy (added)
Comment:
Something prettier than `{% url 'admin:login' %}?next={{
request.path|urlencode }}` would be awesome and make templates more
readable
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:16>
* cc: Carsten Fuchs (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:17>
Comment (by Daniel Rios):
Hi- I am at the sprints of DjangoConEU 2023 and have rediscovered this
ticket I assigned to myself long ago. I am restarting working on it!
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:18>
* cc: Tom Carrick (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:19>
* cc: Sage Abdullah (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/25582#comment:20>