Re: [Django] #10941: Add a templatetag to generate querystrings

30 views
Skip to first unread message

Django

unread,
Oct 23, 2015, 7:54:29 AM10/23/15
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+------------------------------------
Reporter: benspaulding | Owner:
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by spookylukey):

I think this whole area is solved by django-spurl -
https://github.com/j4mie/django-spurl

It builds on urlobject which can be used for Python code:
https://github.com/zacharyvoase/urlobject/

There is also furl for Python code: https://github.com/gruns/furl

I don't think we need to duplicate any of these within Django. Having a
template tag to do 1/10 of what spurl does is just an invitation to
include more and more of spurl's functionality. It might be useful to
have some links to spurl in the documentation.

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

Django

unread,
Feb 15, 2016, 8:26:24 AM2/15/16
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+------------------------------------
Reporter: benspaulding | Owner:
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by funkybob):

Personally I think it wouldn't hurt to have a helper for the specific case
of altering the page number _only_.

In a prior project we ended up needing 4 different querystring
manipulators [and I'm sure there could have been more].

One would output the original, less any listed keys, plus any specified
keys _overridden_ in value.
One would just add new values.
One would return the original with only the listed keys.
A final just updated the page number.

This still doesn't cover, for instance, when you want to add a second
value to an existing key... or.... well, given it's a multi-dict, the
options are endless.

So, to reiterate: A _simple_ helper for helping with pagination, which is
a problem almost everyone encounters, would be good.

A general purpose querystring manipulator? Leave that to 3rd parties.

--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:26>

Django

unread,
Jun 6, 2017, 11:29:43 AM6/6/17
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+------------------------------------
Reporter: Ben Spaulding | Owner: (none)

Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by Jimmy Merrild Krag):

It's been some time since something happened on this one. i have just
stumpled into needing to change query strings in templates, so I thought
I'd add my findings.

I have created two rather simple filter which allows you ro edit the query
string. `django-spurl` already does some of this, however not very
elegantly. I think the following three filters would be a great addition
to Django that will get you "there" much of the time:
{{{
#!python
import six
from django import template
import collections

from django.http import QueryDict

register = template.Library()


@register.simple_tag
def build_query(**kwargs):
"""Build a query string"""
query_dict = QueryDict(mutable=True)

for k, v in kwargs.items():
if isinstance(v, collections.Iterable) and not isinstance(v,
six.string_types):
query_dict.setlist(k, v)
else:
query_dict[k] = v

return query_dict.urlencode()


@register.simple_tag(takes_context=True)
def set_query_values(context, **kwargs):
"""Override existing parameters in the current query string"""
query_dict = context.request.GET.copy()

for k, v in kwargs.items():
if isinstance(v, collections.Iterable) and not isinstance(v,
six.string_types):
query_dict.setlist(k, v)
else:
query_dict[k] = v

return query_dict.urlencode()


@register.simple_tag(takes_context=True)
def append_query_values(context, **kwargs):
"""Append to existing parameters in the current query string"""
query_dict = context.request.GET.copy()

for k, v in kwargs.items():
if isinstance(v, collections.Iterable) and not isinstance(v,
six.string_types):
for v_item in v:
query_dict.appendlist(k, v_item)
else:
query_dict.appendlist(k, v)

return query_dict.urlencode()
}}}

Usage is e.g. `<a href="?{% set_query_values page=page.next_page_number
%}">&raquo;</a>` or `<a href="?{% append_query_values selected=item.id
%}">Select {{ item.name }}</a>`
Note: They all support iterables as values.

I think the only ones probably missing is one for removing a key and one
for removing a specific value from a key.

I'm also thinking of merging the `set` and `append` (also adding `remove`)
by prepending each key with the action and letting `None` clear the list
when using `set_`. I imagine something like `{% modify_query set_level=7
set_nextlevel=None append_consumedlevels=processed_levels_list
remove_availablelevels=7 %}`.

What do you think? Any thoughts?

--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:27>

Django

unread,
Jun 7, 2017, 8:25:02 AM6/7/17
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+------------------------------------
Reporter: Ben Spaulding | Owner: (none)
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by Jimmy Merrild Krag):

I made an implementation of the above mentioned `modify_query`.

[https://pastebin.com/SiDLqs4B]

Thoughts and comments are most welcome.

--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:28>

Django

unread,
Feb 1, 2018, 3:35:00 PM2/1/18
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+------------------------------------
Reporter: Ben Spaulding | Owner: (none)
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Ning):

* cc: Ning (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:29>

Django

unread,
Apr 30, 2018, 2:43:37 PM4/30/18
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+------------------------------------
Reporter: Ben Spaulding | Owner: (none)
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Thomas Capricelli):

* cc: Thomas Capricelli (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:30>

Django

unread,
Dec 21, 2019, 1:04:14 PM12/21/19
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+------------------------------------
Reporter: Ben Spaulding | Owner: (none)
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by Federico Jaramillo Martínez):

Is this ticket still valid?

Would a PR with a templatetag be accepted?
If so, I could be working on this.

If anyone can confirm I would appreciate.

Thanks

--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:31>

Django

unread,
Dec 22, 2019, 10:54:44 AM12/22/19
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+------------------------------------
Reporter: Ben Spaulding | Owner: (none)
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by Claude Paroz):

Sure, it's still valid. Read attentively the existing comments.

Maybe the greatest challenge of this ticket is to find a compromise
between functionality and simplicity, addressing the more common use cases
while accepting it won't address all possible needs.

--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:32>

Django

unread,
Dec 17, 2021, 1:17:35 PM12/17/21
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+------------------------------------
Reporter: Ben Spaulding | Owner: (none)
Type: New feature | Status: new
Component: Template system | Version: dev

Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Carsten Fuchs):

* cc: Carsten Fuchs (added)


Comment:

I too had reinvented this wheel a couple of years ago, unaware of this
ticket. Other implementations above are more powerful, but this one is
short:
{{{
#!python
@register.simple_tag
def query_string(*args, **kwargs):
"""
Combines dictionaries of query parameters and individual query
parameters
and builds an encoded URL query string from the result.
"""
query_dict = QueryDict(mutable=True)

for a in args:
query_dict.update(a)

remove_keys = []

for k, v in kwargs.items():

if v is None:
remove_keys.append(k)
elif isinstance(v, list):


query_dict.setlist(k, v)
else:
query_dict[k] = v

for k in remove_keys:
if k in query_dict:
del query_dict[k]

qs = query_dict.urlencode()
if not qs:
return ""
return "?" + qs
}}}

It cannot do everything, but covers all my use cases: Keep most of the
existing source parameters, but delete, add or change some. Examples:
{{{
{# Just repeat the parameters: #}
{% query_string request.GET %}

{# Add a parameter: #}
{% query_string request.GET format='pdf' %}

{# Change a parameter: #}
{% query_string request.GET page=next_page_number %}

{# Overwrite month and year with precomputed values, e.g. with
next_month_year = {'month': 1, 'year': 2022}, and clear the day: #}
{% query_string request.GET next_month_year day=None %}
}}}

I'm aware that posting yet another implementation does not achieve true
progress and that the more elaborate implementations above may be more
appropriate for inclusion in Django.
Still, I wanted to mention this because in my opinion it keeps the balance
between features and simplicity.

--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:33>

Django

unread,
May 27, 2023, 11:53:22 AM5/27/23
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+------------------------------------
Reporter: Ben Spaulding | Owner: (none)
Type: New feature | Status: new
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by Leonardo Cavallucci):

I would really love to have this: I could work on this, if it is decided
how this should work

--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:34>

Django

unread,
Oct 15, 2023, 3:53:02 AM10/15/23
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+---------------------------------------
Reporter: Ben Spaulding | Owner: Tom Carrick
Type: New feature | Status: assigned

Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+---------------------------------------
Changes (by Tom Carrick):

* owner: (none) => Tom Carrick
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:35>

Django

unread,
Oct 15, 2023, 5:54:54 PM10/15/23
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+---------------------------------------
Reporter: Ben Spaulding | Owner: Tom Carrick
Type: New feature | Status: assigned
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+---------------------------------------
Changes (by Tom Carrick):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/17368 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:36>

Django

unread,
Oct 16, 2023, 3:49:08 AM10/16/23
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+---------------------------------------
Reporter: Ben Spaulding | Owner: Tom Carrick
Type: New feature | Status: assigned
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
---------------------------------+---------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:37>

Django

unread,
Oct 16, 2023, 5:40:19 AM10/16/23
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+---------------------------------------
Reporter: Ben Spaulding | Owner: Tom Carrick
Type: New feature | Status: assigned
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+---------------------------------------
Changes (by Tom Carrick):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:38>

Django

unread,
Oct 16, 2023, 11:44:19 PM10/16/23
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+---------------------------------------
Reporter: Ben Spaulding | Owner: Tom Carrick
Type: New feature | Status: assigned
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
---------------------------------+---------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:39>

Django

unread,
Oct 24, 2023, 7:18:02 AM10/24/23
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
---------------------------------+---------------------------------------
Reporter: Ben Spaulding | Owner: Tom Carrick
Type: New feature | Status: assigned
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+---------------------------------------
Changes (by Tom Carrick):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:40>

Django

unread,
Oct 26, 2023, 3:56:32 AM10/26/23
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
-------------------------------------+-------------------------------------

Reporter: Ben Spaulding | Owner: Tom
| Carrick
Type: New feature | Status: assigned
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:41>

Django

unread,
Oct 26, 2023, 5:53:15 AM10/26/23
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
-------------------------------------+-------------------------------------
Reporter: Ben Spaulding | Owner: Tom
| Carrick
Type: New feature | Status: closed

Component: Template system | Version: dev
Severity: Normal | Resolution: fixed

Keywords: pagination | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"e67d3580edbee1a4b58d40875293714ac3fc6937" e67d3580]:
{{{
#!CommitTicketReference repository=""
revision="e67d3580edbee1a4b58d40875293714ac3fc6937"
Fixed #10941 -- Added {% query_string %} template tag.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:42>

Django

unread,
Jul 15, 2024, 12:29:08 PM (2 days ago) Jul 15
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
-------------------------------------+-------------------------------------
Reporter: Ben Spaulding | Owner: Tom
| Carrick
Type: New feature | Status: closed
Component: Template system | Version: dev
Severity: Normal | Resolution: fixed
Keywords: pagination | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by GitHub <noreply@…>):

In [changeset:"27043bde5b795eb4a605aeca1d3bc4345d2ca478" 27043bd]:
{{{#!CommitTicketReference repository=""
revision="27043bde5b795eb4a605aeca1d3bc4345d2ca478"
Refs #10941 -- Renamed query_string template tag to querystring.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:43>

Django

unread,
Jul 15, 2024, 12:30:30 PM (2 days ago) Jul 15
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
-------------------------------------+-------------------------------------
Reporter: Ben Spaulding | Owner: Tom
| Carrick
Type: New feature | Status: closed
Component: Template system | Version: dev
Severity: Normal | Resolution: fixed
Keywords: pagination | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia <124304+nessita@…>):

In [changeset:"91a5b5a4bbc20f5033060ba43f505c667270ecd3" 91a5b5a]:
{{{#!CommitTicketReference repository=""
revision="91a5b5a4bbc20f5033060ba43f505c667270ecd3"
[5.1.x] Refs #10941 -- Renamed query_string template tag to querystring.

Backport of 27043bde5b795eb4a605aeca1d3bc4345d2ca478 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:44>

Django

unread,
Jul 16, 2024, 9:15:01 PM (22 hours ago) Jul 16
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
-------------------------------------+-------------------------------------
Reporter: Ben Spaulding | Owner: Tom
| Carrick
Type: New feature | Status: closed
Component: Template system | Version: dev
Severity: Normal | Resolution: fixed
Keywords: pagination | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by GitHub <noreply@…>):

In [changeset:"5dc17177c38662d6f4408258ee117cd80e0cb933" 5dc1717]:
{{{#!CommitTicketReference repository=""
revision="5dc17177c38662d6f4408258ee117cd80e0cb933"
Refs #10941 -- Renamed test file test_query_string.py to
test_querystring.py.

This follows previous renames made in
27043bde5b795eb4a605aeca1d3bc4345d2ca478.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:45>

Django

unread,
Jul 16, 2024, 9:18:22 PM (22 hours ago) Jul 16
to django-...@googlegroups.com
#10941: Add a templatetag to generate querystrings
-------------------------------------+-------------------------------------
Reporter: Ben Spaulding | Owner: Tom
| Carrick
Type: New feature | Status: closed
Component: Template system | Version: dev
Severity: Normal | Resolution: fixed
Keywords: pagination | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia <124304+nessita@…>):

In [changeset:"df7ebb8b0205273f71c60c33a5de0b680d6a3849" df7ebb8]:
{{{#!CommitTicketReference repository=""
revision="df7ebb8b0205273f71c60c33a5de0b680d6a3849"
[5.1.x] Refs #10941 -- Renamed test file test_query_string.py to
test_querystring.py.

This follows previous renames made in
27043bde5b795eb4a605aeca1d3bc4345d2ca478.

Backport of 5dc17177c38662d6f4408258ee117cd80e0cb933 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:46>
Reply all
Reply to author
Forward
0 new messages