summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorBen Cardy <benbacardi@gmail.com>2024-12-11 19:40:28 +0000
committerGitHub <noreply@github.com>2024-12-11 16:40:28 -0300
commitf30b527f170fd4ec9bf10b5012e1ef79776a8f8e (patch)
treeec361a89467f035d5fbccc6b5834743ed7260bb4 /docs
parent2ce4545de1791d6ed2405cb0657401e179bc5357 (diff)
Fixed #25582 -- Added support for query and fragment to django.urls.reverse().
Diffstat (limited to 'docs')
-rw-r--r--docs/ref/urlresolvers.txt29
-rw-r--r--docs/releases/5.2.txt4
2 files changed, 31 insertions, 2 deletions
diff --git a/docs/ref/urlresolvers.txt b/docs/ref/urlresolvers.txt
index eca00cf106..0c26f9578a 100644
--- a/docs/ref/urlresolvers.txt
+++ b/docs/ref/urlresolvers.txt
@@ -10,7 +10,7 @@
The ``reverse()`` function can be used to return an absolute path reference
for a given view and optional parameters, similar to the :ttag:`url` tag:
-.. function:: reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)
+.. function:: reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None, *, query=None, fragment=None)
``viewname`` can be a :ref:`URL pattern name <naming-url-patterns>` or the
callable view object used in the URLconf. For example, given the following
@@ -67,6 +67,33 @@ namespaces into URLs on specific application instances, according to the
The ``urlconf`` argument is the URLconf module containing the URL patterns to
use for reversing. By default, the root URLconf for the current thread is used.
+The ``query`` keyword argument specifies parameters to be added to the returned
+URL. It can accept an instance of :class:`~django.http.QueryDict` (such as
+``request.GET``) or any value compatible with :func:`urllib.parse.urlencode`.
+The encoded query string is appended to the resolved URL, prefixed by a ``?``.
+
+The ``fragment`` keyword argument specifies a fragment identifier to be
+appended to the returned URL (that is, after the path and query string,
+preceded by a ``#``).
+
+For example:
+
+.. code-block:: pycon
+
+ >>> from django.urls import reverse
+ >>> reverse("admin:index", query={"q": "biscuits", "page": 2}, fragment="results")
+ '/admin/?q=biscuits&page=2#results'
+ >>> reverse("admin:index", query=[("color", "blue"), ("color", 1), ("none", None)])
+ '/admin/?color=blue&color=1&none=None'
+ >>> reverse("admin:index", query={"has empty spaces": "also has empty spaces!"})
+ '/admin/?has+empty+spaces=also+has+empty+spaces%21'
+ >>> reverse("admin:index", fragment="no encoding is done")
+ '/admin/#no encoding is done'
+
+.. versionchanged:: 5.2
+
+ The ``query`` and ``fragment`` arguments were added.
+
.. note::
The string returned by ``reverse()`` is already
diff --git a/docs/releases/5.2.txt b/docs/releases/5.2.txt
index 5e692c0345..58d5727f32 100644
--- a/docs/releases/5.2.txt
+++ b/docs/releases/5.2.txt
@@ -370,7 +370,9 @@ Tests
URLs
~~~~
-* ...
+* :func:`~django.urls.reverse` now accepts ``query`` and ``fragment`` keyword
+ arguments, allowing the addition of a query string and/or fragment identifier
+ in the generated URL, respectively.
Utilities
~~~~~~~~~