[5.1.x] Refs #10941 -- Renamed query_string template tag to querystring.

Backport of 27043bde5b from main.
This commit is contained in:
Sarah Boyce 2024-07-15 18:28:55 +02:00 committed by Natalia
parent bdfcda8c26
commit 91a5b5a4bb
4 changed files with 51 additions and 55 deletions

View File

@ -1169,8 +1169,8 @@ def now(parser, token):
return NowNode(format_string, asvar)
@register.simple_tag(takes_context=True)
def query_string(context, query_dict=None, **kwargs):
@register.simple_tag(name="querystring", takes_context=True)
def querystring(context, query_dict=None, **kwargs):
"""
Add, remove, and change parameters of a ``QueryDict`` and return the result
as a query string. If the ``query_dict`` argument is not provided, default
@ -1178,19 +1178,19 @@ def query_string(context, query_dict=None, **kwargs):
For example::
{% query_string foo=3 %}
{% querystring foo=3 %}
To remove a key::
{% query_string foo=None %}
{% querystring foo=None %}
To use with pagination::
{% query_string page=page_obj.next_page_number %}
{% querystring page=page_obj.next_page_number %}
A custom ``QueryDict`` can also be used::
{% query_string my_query_dict foo=3 %}
{% querystring my_query_dict foo=3 %}
"""
if query_dict is None:
query_dict = context.request.GET

View File

@ -952,9 +952,9 @@ output (as a string) inside a variable. This is useful if you want to use
{% now "Y" as current_year %}
{% blocktranslate %}Copyright {{ current_year }}{% endblocktranslate %}
.. templatetag:: query_string
.. templatetag:: querystring
``query_string``
``querystring``
----------------
.. versionadded:: 5.1
@ -975,14 +975,14 @@ query string is ``?color=green&size=M``, the output would be
.. code-block:: html+django
{% query_string %}
{% querystring %}
You can also pass in a custom ``QueryDict`` that will be used instead of
``request.GET``:
.. code-block:: html+django
{% query_string my_query_dict %}
{% querystring my_query_dict %}
Each keyword argument will be added to the query string, replacing any existing
value for that key. With the query string ``?color=blue``, the following would
@ -990,14 +990,14 @@ result in ``?color=red&size=S``:
.. code-block:: html+django
{% query_string color="red" size="S" %}
{% querystring color="red" size="S" %}
It is possible to remove parameters by passing ``None`` as a value. With the
query string ``?color=blue&size=M``, the following would result in ``?size=M``:
.. code-block:: html+django
{% query_string color=None %}
{% querystring color=None %}
If the given parameter is a list, the value will remain as a list. For example,
if ``my_list`` is set to ``["red", "blue"]``, the following would result in
@ -1005,7 +1005,7 @@ if ``my_list`` is set to ``["red", "blue"]``, the following would result in
.. code-block:: html+django
{% query_string color=my_list %}
{% querystring color=my_list %}
A common example of using this tag is to preserve the current query string when
displaying a page of results, while adding a link to the next and previous
@ -1015,14 +1015,14 @@ would output ``?color=blue&size=M&page=4``:
.. code-block:: html+django
{% query_string page=page.next_page_number %}
{% querystring page=page.next_page_number %}
You can also store the value in a variable, for example, if you need multiple
links to the same page with syntax such as:
.. code-block:: html+django
{% query_string page=page.next_page_number as next_page %}
{% querystring page=page.next_page_number as next_page %}
.. templatetag:: regroup

View File

@ -26,10 +26,10 @@ only officially support the latest release of each series.
What's new in Django 5.1
========================
``{% query_string %}`` template tag
``{% querystring %}`` template tag
-----------------------------------
Django 5.1 introduces the :ttag:`{% query_string %} <query_string>` template
Django 5.1 introduces the :ttag:`{% querystring %} <querystring>` template
tag, simplifying the modification of query parameters in URLs, making it easier
to generate links that maintain existing query parameters while adding or
changing specific ones.
@ -53,7 +53,7 @@ When switching to using this new template tag, the above magically becomes:
.. code-block:: html+django
<a href="{% query_string page=page.next_page_number %}">Next page</a>
<a href="{% querystring page=page.next_page_number %}">Next page</a>
PostgreSQL Connection Pools
---------------------------

View File

@ -9,92 +9,88 @@ class QueryStringTagTests(SimpleTestCase):
def setUp(self):
self.request_factory = RequestFactory()
@setup({"query_string_empty": "{% query_string %}"})
def test_query_string_empty(self):
@setup({"querystring_empty": "{% querystring %}"})
def test_querystring_empty(self):
request = self.request_factory.get("/")
template = self.engine.get_template("query_string_empty")
template = self.engine.get_template("querystring_empty")
context = RequestContext(request)
output = template.render(context)
self.assertEqual(output, "")
@setup({"query_string_non_empty": "{% query_string %}"})
def test_query_string_non_empty(self):
@setup({"querystring_non_empty": "{% querystring %}"})
def test_querystring_non_empty(self):
request = self.request_factory.get("/", {"a": "b"})
template = self.engine.get_template("query_string_non_empty")
template = self.engine.get_template("querystring_non_empty")
context = RequestContext(request)
output = template.render(context)
self.assertEqual(output, "?a=b")
@setup({"query_string_multiple": "{% query_string %}"})
def test_query_string_multiple(self):
@setup({"querystring_multiple": "{% querystring %}"})
def test_querystring_multiple(self):
request = self.request_factory.get("/", {"x": "y", "a": "b"})
template = self.engine.get_template("query_string_multiple")
template = self.engine.get_template("querystring_multiple")
context = RequestContext(request)
output = template.render(context)
self.assertEqual(output, "?x=y&amp;a=b")
@setup({"query_string_replace": "{% query_string a=1 %}"})
def test_query_string_replace(self):
@setup({"querystring_replace": "{% querystring a=1 %}"})
def test_querystring_replace(self):
request = self.request_factory.get("/", {"x": "y", "a": "b"})
template = self.engine.get_template("query_string_replace")
template = self.engine.get_template("querystring_replace")
context = RequestContext(request)
output = template.render(context)
self.assertEqual(output, "?x=y&amp;a=1")
@setup({"query_string_add": "{% query_string test_new='something' %}"})
def test_query_string_add(self):
@setup({"querystring_add": "{% querystring test_new='something' %}"})
def test_querystring_add(self):
request = self.request_factory.get("/", {"a": "b"})
template = self.engine.get_template("query_string_add")
template = self.engine.get_template("querystring_add")
context = RequestContext(request)
output = template.render(context)
self.assertEqual(output, "?a=b&amp;test_new=something")
@setup({"query_string_remove": "{% query_string test=None a=1 %}"})
def test_query_string_remove(self):
@setup({"querystring_remove": "{% querystring test=None a=1 %}"})
def test_querystring_remove(self):
request = self.request_factory.get("/", {"test": "value", "a": "1"})
template = self.engine.get_template("query_string_remove")
template = self.engine.get_template("querystring_remove")
context = RequestContext(request)
output = template.render(context)
self.assertEqual(output, "?a=1")
@setup(
{"query_string_remove_nonexistent": "{% query_string nonexistent=None a=1 %}"}
)
def test_query_string_remove_nonexistent(self):
@setup({"querystring_remove_nonexistent": "{% querystring nonexistent=None a=1 %}"})
def test_querystring_remove_nonexistent(self):
request = self.request_factory.get("/", {"x": "y", "a": "1"})
template = self.engine.get_template("query_string_remove_nonexistent")
template = self.engine.get_template("querystring_remove_nonexistent")
context = RequestContext(request)
output = template.render(context)
self.assertEqual(output, "?x=y&amp;a=1")
@setup({"query_string_list": "{% query_string a=my_list %}"})
def test_query_string_add_list(self):
@setup({"querystring_list": "{% querystring a=my_list %}"})
def test_querystring_add_list(self):
request = self.request_factory.get("/")
template = self.engine.get_template("query_string_list")
template = self.engine.get_template("querystring_list")
context = RequestContext(request, {"my_list": [2, 3]})
output = template.render(context)
self.assertEqual(output, "?a=2&amp;a=3")
@setup({"query_string_query_dict": "{% query_string request.GET a=2 %}"})
def test_query_string_with_explicit_query_dict(self):
@setup({"querystring_query_dict": "{% querystring request.GET a=2 %}"})
def test_querystring_with_explicit_query_dict(self):
request = self.request_factory.get("/", {"a": 1})
output = self.engine.render_to_string(
"query_string_query_dict", {"request": request}
"querystring_query_dict", {"request": request}
)
self.assertEqual(output, "?a=2")
@setup(
{"query_string_query_dict_no_request": "{% query_string my_query_dict a=2 %}"}
)
def test_query_string_with_explicit_query_dict_and_no_request(self):
@setup({"querystring_query_dict_no_request": "{% querystring my_query_dict a=2 %}"})
def test_querystring_with_explicit_query_dict_and_no_request(self):
context = {"my_query_dict": QueryDict("a=1&b=2")}
output = self.engine.render_to_string(
"query_string_query_dict_no_request", context
"querystring_query_dict_no_request", context
)
self.assertEqual(output, "?a=2&amp;b=2")
@setup({"query_string_no_request_no_query_dict": "{% query_string %}"})
def test_query_string_without_request_or_explicit_query_dict(self):
@setup({"querystring_no_request_no_query_dict": "{% querystring %}"})
def test_querystring_without_request_or_explicit_query_dict(self):
msg = "'Context' object has no attribute 'request'"
with self.assertRaisesMessage(AttributeError, msg):
self.engine.render_to_string("query_string_no_request_no_query_dict")
self.engine.render_to_string("querystring_no_request_no_query_dict")