1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #28999 -- Documented how to reverse a class-based view by instance.

Co-authored-by: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com>
This commit is contained in:
Clifford Gama
2024-10-22 14:12:02 +02:00
committed by Sarah Boyce
parent be138f32ed
commit 4d11ea1ef0
4 changed files with 27 additions and 6 deletions

View File

@@ -13,7 +13,8 @@ your code, Django provides the following function:
.. function:: reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)
``viewname`` can be a :ref:`URL pattern name <naming-url-patterns>` or the
callable view object. For example, given the following ``url``::
callable view object used in the URLconf. For example, given the following
``url``::
from news import views
@@ -79,6 +80,26 @@ use for reversing. By default, the root URLconf for the current thread is used.
Applying further encoding (such as :func:`urllib.parse.quote`) to the output
of ``reverse()`` may produce undesirable results.
.. admonition:: Reversing class-based views by view object
The view object can also be the result of calling
:meth:`~django.views.generic.base.View.as_view` if the same view object is
used in the URLConf. Following the original example, the view object could
be defined as:
.. code-block:: python
:caption: ``news/views.py``
from django.views import View
class ArchiveView(View): ...
archive = ArchiveView.as_view()
However, remember that namespaced views cannot be reversed by view object.
``reverse_lazy()``
==================