Skip to content

Commit 6511340

Browse files
committed
[5.1.x] Fixed #36182 -- Returned "?" if all parameters are removed in querystring template tag.
Thank you to David Feeley for the report and Natalia Bidart for the review. Backport of 05002c1 from main.
1 parent 861f9a2 commit 6511340

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

django/template/defaulttags.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,18 +1194,18 @@ def querystring(context, query_dict=None, **kwargs):
11941194
"""
11951195
if query_dict is None:
11961196
query_dict = context.request.GET
1197-
query_dict = query_dict.copy()
1197+
params = query_dict.copy()
11981198
for key, value in kwargs.items():
11991199
if value is None:
1200-
if key in query_dict:
1201-
del query_dict[key]
1200+
if key in params:
1201+
del params[key]
12021202
elif isinstance(value, Iterable) and not isinstance(value, str):
1203-
query_dict.setlist(key, value)
1203+
params.setlist(key, value)
12041204
else:
1205-
query_dict[key] = value
1206-
if not query_dict:
1205+
params[key] = value
1206+
if not params and not query_dict:
12071207
return ""
1208-
query_string = query_dict.urlencode()
1208+
query_string = params.urlencode()
12091209
return f"?{query_string}"
12101210

12111211

docs/ref/templates/builtins.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ This tag requires a :class:`~django.http.QueryDict` instance, which defaults to
965965
:attr:`request.GET <django.http.HttpRequest.GET>` if none is provided.
966966

967967
If the :class:`~django.http.QueryDict` is empty and no additional parameters
968-
are provided, an empty string is returned. A non-empty result includes a
968+
are provided, an empty string is returned. Otherwise, the result includes a
969969
leading ``"?"``.
970970

971971
.. admonition:: Using ``request.GET`` as default

docs/releases/5.1.7.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ Django 5.1.7 fixes several bugs in 5.1.6.
99
Bugfixes
1010
========
1111

12-
* ...
12+
* Fixed a bug in Django 5.1 where the ``{% querystring %}`` template tag
13+
returned an empty string rather than ``"?"`` when all parameters had been
14+
removed from the query string (:ticket:`36182`).

tests/template_tests/syntax_tests/test_querystring.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ def test_querystring_empty(self):
1717
output = template.render(context)
1818
self.assertEqual(output, "")
1919

20+
@setup({"test_querystring_remove_all_params": "{% querystring a=None %}"})
21+
def test_querystring_remove_all_params(self):
22+
non_empty_context = RequestContext(self.request_factory.get("/?a=b"))
23+
empty_context = RequestContext(self.request_factory.get("/"))
24+
template = self.engine.get_template("test_querystring_remove_all_params")
25+
for context, expected in [(non_empty_context, "?"), (empty_context, "")]:
26+
with self.subTest(expected=expected):
27+
self.assertEqual(template.render(context), expected)
28+
2029
@setup({"querystring_non_empty": "{% querystring %}"})
2130
def test_querystring_non_empty(self):
2231
request = self.request_factory.get("/", {"a": "b"})

0 commit comments

Comments
 (0)