1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

[5.1.x] Refs #10941 -- Reorganized querystring template tag docs.

Backport of cf03aa4e94625971852a09e869f7ee7c328b573f from main.
This commit is contained in:
nessita 2024-07-22 10:31:54 -03:00 committed by Natalia
parent 8fb7d30456
commit 39062e7946

View File

@ -959,66 +959,99 @@ output (as a string) inside a variable. This is useful if you want to use
.. versionadded:: 5.1 .. versionadded:: 5.1
Outputs the query string from a given :class:`~django.http.QueryDict` instance, Outputs a URL-encoded formatted query string based on the provided parameters.
if provided, or ``request.GET`` if not and the
``django.template.context_processors.request`` context processor is enabled.
If the ``QueryDict`` is empty, then the output will be an empty string.
Otherwise, the query string will be returned with a leading ``"?"``.
If not using the ``django.template.context_processors.request`` context This tag requires a :class:`~django.http.QueryDict` instance, which defaults to
processor, you must pass either the ``request`` into the template context or a :attr:`request.GET <django.http.HttpRequest.GET>` if none is provided.
``QueryDict`` instance into this tag.
The following example outputs the current query string verbatim. So if the If the :class:`~django.http.QueryDict` is empty and no additional parameters
query string is ``?color=green&size=M``, the output would be are provided, an empty string is returned. A non-empty result includes a
``?color=green&size=M``: leading ``"?"``.
.. admonition:: Using ``request.GET`` as default
To use ``request.GET`` as the default ``QueryDict`` instance, the
``django.template.context_processors.request`` context processor should be
enabled. If it's not enabled, you must either explicitly pass the
``request`` object into the template context, or provide a ``QueryDict``
instance to this tag.
Basic usage
~~~~~~~~~~~
.. code-block:: html+django .. code-block:: html+django
{% querystring %} {% querystring %}
You can also pass in a custom ``QueryDict`` that will be used instead of Outputs the current query string verbatim. So if the query string is
``request.GET``: ``?color=green``, the output would be ``?color=green``.
.. code-block:: html+django
{% querystring size="M" %}
Outputs the current query string with the addition of the ``size`` parameter.
Following the previous example, the output would be ``?color=green&size=M``.
Custom QueryDict
~~~~~~~~~~~~~~~~
.. code-block:: html+django .. code-block:: html+django
{% querystring my_query_dict %} {% querystring my_query_dict %}
Each keyword argument will be added to the query string, replacing any existing You can provide a custom ``QueryDict`` to be used instead of ``request.GET``.
value for that key. With the query string ``?color=blue``, the following would So if ``my_query_dict`` is ``<QueryDict: {'color': ['blue']}>``, this outputs
result in ``?color=red&size=S``: ``?color=blue``.
Setting items
~~~~~~~~~~~~~
.. code-block:: html+django .. code-block:: html+django
{% querystring color="red" size="S" %} {% querystring color="red" size="S" %}
It is possible to remove parameters by passing ``None`` as a value. With the Adds or modifies parameters in the query string. Each keyword argument will be
query string ``?color=blue&size=M``, the following would result in ``?size=M``: added to the query string, replacing any existing value for that key. For
instance, if the current query string is ``?color=green``, the output will be
``?color=red&size=S``.
Removing items
~~~~~~~~~~~~~~
.. code-block:: html+django .. code-block:: html+django
{% querystring color=None %} {% querystring color=None %}
If the given parameter is a list, the value will remain as a list. For example, Passing ``None`` as the value removes the parameter from the query string. For
if ``my_list`` is set to ``["red", "blue"]``, the following would result in example, if the current query string is ``?color=green&size=M``, the output
``?color=red&color=blue``: will be ``?size=M``.
Handling lists
~~~~~~~~~~~~~~
.. code-block:: html+django .. code-block:: html+django
{% querystring color=my_list %} {% querystring color=my_list %}
If ``my_list`` is ``["red", "blue"]``, the output will be
``?color=red&color=blue``, preserving the list structure in the query string.
Dynamic usage
~~~~~~~~~~~~~
A common example of using this tag is to preserve the current query string when 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 displaying a page of results, while adding a link to the next and previous
pages of results. For example, if the paginator is currently on page 3, and pages of results. For example, if the paginator is currently on page 3, and the
the current query string is ``?color=blue&size=M&page=3``, the following code current query string is ``?color=blue&size=M&page=3``, the following code would
would output ``?color=blue&size=M&page=4``: output ``?color=blue&size=M&page=4``:
.. code-block:: html+django .. code-block:: html+django
{% querystring 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 You can also store the value in a variable. For example, if you need multiple
links to the same page with syntax such as: links to the same page, define it as:
.. code-block:: html+django .. code-block:: html+django