Skip to content

Commit

Permalink
Refs django#10941 -- Renamed query_string template tag to querystring.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahboyce authored Jul 15, 2024
1 parent 65344f0 commit 27043bd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 55 deletions.
12 changes: 6 additions & 6 deletions django/template/defaulttags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,28 +1169,28 @@ 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
to ``request.GET``.
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
Expand Down
18 changes: 9 additions & 9 deletions docs/ref/templates/builtins.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -975,37 +975,37 @@ 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
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
``?color=red&color=blue``:

.. 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
Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions docs/releases/5.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
---------------------------
Expand Down
70 changes: 33 additions & 37 deletions tests/template_tests/syntax_tests/test_query_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 27043bd

Please sign in to comment.