From a39c28706aa7a8c3effd0980ae6d59ae67299d85 Mon Sep 17 00:00:00 2001 From: Giannis Terzopoulos Date: Wed, 19 Mar 2025 12:21:41 +0100 Subject: Fixed #35529 -- Added support for positional arguments in querystring template tag. Co-authored-by: Natalia <124304+nessita@users.noreply.github.com> --- docs/ref/templates/builtins.txt | 39 +++++++++++++++++++++++++++++++-------- docs/releases/6.0.txt | 4 ++++ 2 files changed, 35 insertions(+), 8 deletions(-) (limited to 'docs') diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index 849faaa46f..f3d967b153 100644 --- a/docs/ref/templates/builtins.txt +++ b/docs/ref/templates/builtins.txt @@ -964,8 +964,14 @@ output (as a string) inside a variable. This is useful if you want to use Outputs a URL-encoded formatted query string based on the provided parameters. -This tag requires a :class:`~django.http.QueryDict` instance, which defaults to -:attr:`request.GET ` if none is provided. +This tag accepts positional arguments, which must be mappings (such as +:class:`~django.http.QueryDict` or :class:`dict`). If no positional arguments +are provided, :attr:`request.GET ` is used as the +default to construct the query string. + +Positional arguments are processed sequentially, while keyword arguments are +treated as key-value pairs, applied last. Later arguments take precedence over +earlier ones, ensuring the most recent pairs are reflected in the final result. The result always includes a leading ``"?"`` since this tag is mainly used for links, and an empty result could prevent the page from reloading as expected. @@ -1033,16 +1039,33 @@ Handling lists If ``my_list`` is ``["red", "blue"]``, the output will be ``?color=red&color=blue``, preserving the list structure in the query string. -Custom QueryDict -~~~~~~~~~~~~~~~~ +Customizing the base QueryDict +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can pass custom ``QueryDict`` or ``dict`` instances as positional arguments +to replace ``request.GET``. When multiple arguments are provided, key-value +pairs from later arguments take precedence over earlier ones. + +For example, if ``my_query_dict`` is ```` and ``my_dict`` is ``{'color': 'orange', 'fabric': 'silk', 'type': +'dress'}``, this outputs ``?color=orange&size=S&fabric=silk``. + +.. code-block:: html+django + + {% querystring my_query_dict my_dict size="S" type=None %} + +If all keys are removed by setting them to ``None``, this outputs ``?``: .. code-block:: html+django - {% querystring my_query_dict %} + {% querystring my_query_dict my_dict color=None size=None fabric=None type=None %} + +Similarly, if all positional arguments are empty and keyword arguments do not +contribute any new params, the output will also be ``?``. + +.. versionchanged:: 6.0 -You can provide a custom ``QueryDict`` to be used instead of ``request.GET``. -So if ``my_query_dict`` is ````, this outputs -``?color=blue``. If ``my_query_dict`` is empty, the output will be ``?``. + Support for multiple positional mapping arguments was added. Dynamic usage ~~~~~~~~~~~~~ diff --git a/docs/releases/6.0.txt b/docs/releases/6.0.txt index cd72185354..499163788f 100644 --- a/docs/releases/6.0.txt +++ b/docs/releases/6.0.txt @@ -236,6 +236,10 @@ Templates * The :ttag:`querystring` template tag now consistently prefixes the returned query string with a ``?``, ensuring reliable link generation behavior. +* The :ttag:`querystring` template tag now accepts multiple positional + arguments, which must be mappings, such as :class:`~django.http.QueryDict` + or :class:`dict`. + Tests ~~~~~ -- cgit v1.3