mirror of
https://github.com/django/django.git
synced 2025-03-31 19:46:42 +00:00
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.
This commit is contained in:
parent
0ee842bb45
commit
05002c153c
@ -1194,18 +1194,18 @@ def querystring(context, query_dict=None, **kwargs):
|
|||||||
"""
|
"""
|
||||||
if query_dict is None:
|
if query_dict is None:
|
||||||
query_dict = context.request.GET
|
query_dict = context.request.GET
|
||||||
query_dict = query_dict.copy()
|
params = query_dict.copy()
|
||||||
for key, value in kwargs.items():
|
for key, value in kwargs.items():
|
||||||
if value is None:
|
if value is None:
|
||||||
if key in query_dict:
|
if key in params:
|
||||||
del query_dict[key]
|
del params[key]
|
||||||
elif isinstance(value, Iterable) and not isinstance(value, str):
|
elif isinstance(value, Iterable) and not isinstance(value, str):
|
||||||
query_dict.setlist(key, value)
|
params.setlist(key, value)
|
||||||
else:
|
else:
|
||||||
query_dict[key] = value
|
params[key] = value
|
||||||
if not query_dict:
|
if not params and not query_dict:
|
||||||
return ""
|
return ""
|
||||||
query_string = query_dict.urlencode()
|
query_string = params.urlencode()
|
||||||
return f"?{query_string}"
|
return f"?{query_string}"
|
||||||
|
|
||||||
|
|
||||||
|
@ -963,7 +963,7 @@ This tag requires a :class:`~django.http.QueryDict` instance, which defaults to
|
|||||||
:attr:`request.GET <django.http.HttpRequest.GET>` if none is provided.
|
:attr:`request.GET <django.http.HttpRequest.GET>` if none is provided.
|
||||||
|
|
||||||
If the :class:`~django.http.QueryDict` is empty and no additional parameters
|
If the :class:`~django.http.QueryDict` is empty and no additional parameters
|
||||||
are provided, an empty string is returned. A non-empty result includes a
|
are provided, an empty string is returned. Otherwise, the result includes a
|
||||||
leading ``"?"``.
|
leading ``"?"``.
|
||||||
|
|
||||||
.. admonition:: Using ``request.GET`` as default
|
.. admonition:: Using ``request.GET`` as default
|
||||||
|
@ -9,4 +9,6 @@ Django 5.1.7 fixes several bugs in 5.1.6.
|
|||||||
Bugfixes
|
Bugfixes
|
||||||
========
|
========
|
||||||
|
|
||||||
* ...
|
* Fixed a bug in Django 5.1 where the ``{% querystring %}`` template tag
|
||||||
|
returned an empty string rather than ``"?"`` when all parameters had been
|
||||||
|
removed from the query string (:ticket:`36182`).
|
||||||
|
@ -20,6 +20,18 @@ class QueryStringTagTests(SimpleTestCase):
|
|||||||
"test_querystring_empty_get_params", context, expected=""
|
"test_querystring_empty_get_params", context, expected=""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@setup({"test_querystring_remove_all_params": "{% querystring a=None %}"})
|
||||||
|
def test_querystring_remove_all_params(self):
|
||||||
|
non_empty_context = RequestContext(self.request_factory.get("/?a=b"))
|
||||||
|
empty_context = RequestContext(self.request_factory.get("/"))
|
||||||
|
for context, expected in [(non_empty_context, "?"), (empty_context, "")]:
|
||||||
|
with self.subTest(expected=expected):
|
||||||
|
self.assertRenderEqual(
|
||||||
|
"test_querystring_remove_all_params",
|
||||||
|
context,
|
||||||
|
expected,
|
||||||
|
)
|
||||||
|
|
||||||
@setup({"test_querystring_non_empty_get_params": "{% querystring %}"})
|
@setup({"test_querystring_non_empty_get_params": "{% querystring %}"})
|
||||||
def test_querystring_non_empty_get_params(self):
|
def test_querystring_non_empty_get_params(self):
|
||||||
request = self.request_factory.get("/", {"a": "b"})
|
request = self.request_factory.get("/", {"a": "b"})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user