#36268: Always render the empty querystring templatetag should render empty
querystring as "?" not ""
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Type:
| Cleanup/optimization
Status: new | Component: Template
| system
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Test to demonstrate the issue:
{{{#!diff
--- a/tests/template_tests/syntax_tests/test_querystring.py
+++ b/tests/template_tests/syntax_tests/test_querystring.py
@@ -56,6 +56,13 @@ class QueryStringTagTests(SimpleTestCase):
self.assertRenderEqual(
"test_querystring_empty_params", context, expected=""
)
+ request = self.request_factory.get("/?a=1")
+ for param in cases:
+ with self.subTest(param=param, url="/?a=1"):
+ context = RequestContext(request, {"qd": param})
+ self.assertRenderEqual(
+ "test_querystring_empty_params", context,
expected="?"
+ )
@setup({"querystring_replace": "{% querystring a=1 %}"})
}}}
In short, returning an empty string for querydict means that the page
would not reload if this was used in a link, see #36182.
Therefore, it only makes sense to return an empty string when the query
string that would be evaluated is equivalent to the current query string.
We are not supporting this in most cases e.g. if you are on
`/?color=green` and in the template there is `{% querystring color=green
%}` this evaluates to `"?color=green"` (not `""`).
In the niche case that you are on `/?color=green` and pass in an empty
query dict rather than the default `request.context.GET`, and render `{%
querystring query_dict %}` this returns `""` and so the page would not
reload and you would stay on `/?color=green` even though you probably
expected this to blitz the query string and take you to `/` (which would
be the case if `"?"` was returned).
I think this is niche because there is no reason for you not to hard code
the url in these cases as you don't care about the current query string
For simplicity, I think the proceeding "?" should always be returned.
I think the "bug" I described is unlikely to happen in the wild, I am
classing this as a cleanup. Other thoughts are welcome.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36268>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.