Opened 9 years ago
Last modified 4 days ago
#25582 new New feature
Add a way to build URLs with query strings
Reported by: | Thomas Güttler | Owned by: | |
---|---|---|---|
Component: | Core (URLs) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Ning, Herbert Fortes, Will Gordon, thenewguy, Carsten Fuchs, Tom Carrick, Sage Abdullah | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
It is a common question on stackoverflow and other places:
How to reverse() to url including GET parameters? Example: .../myview?foo=bar
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.
Change History (21)
comment:1 by , 9 years ago
Summary: | reverse() to url including GET parameters: .../myview?foo=bar → Add a way to build URLs with query strings |
---|---|
Triage Stage: | Unreviewed → Accepted |
Version: | 1.8 → master |
comment:2 by , 9 years ago
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.
follow-up: 6 comment:3 by , 9 years ago
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
follow-up: 7 comment:4 by , 9 years ago
@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.
comment:6 by , 9 years ago
Replying to 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.
comment:7 by , 9 years ago
Replying to 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 urlIt 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
comment:8 by , 9 years ago
The 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()
.
comment:9 by , 7 years ago
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?
comment:10 by , 7 years ago
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.
comment:11 by , 7 years ago
Cc: | added |
---|
comment:12 by , 6 years ago
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? Based on the idea of Thomas Güttler, I was thinking parameters like get__{name}=
would evaluate for the query string, fragmet=
to the fragment URL, and get__dict=
would accept a whole dict at once. That way, it is backwards compatible while also wrapping a whole new world.
comment:13 by , 6 years ago
Cc: | added |
---|
comment:14 by , 6 years ago
Cc: | added |
---|
comment:15 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:16 by , 5 years ago
Cc: | added |
---|
Something prettier than {% url 'admin:login' %}?next={{ request.path|urlencode }}
would be awesome and make templates more readable
comment:17 by , 3 years ago
Cc: | added |
---|
comment:18 by , 17 months ago
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!
comment:19 by , 12 months ago
Cc: | added |
---|
comment:20 by , 12 months ago
Cc: | added |
---|
comment:21 by , 4 days ago
Owner: | removed |
---|---|
Status: | assigned → new |
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.