8000 Fixed #28999 -- Documented how to reverse a class-based view by insta… · commit-0/django@4d11ea1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d11ea1

Browse files
cliff688sarahboyce
andcommitted
Fixed #28999 -- Documented how to reverse a class-based view by instance.
Co-authored-by: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com>
1 parent be138f3 commit 4d11ea1

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

docs/ref/urlresolvers.txt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ your code, Django provides the following function:
1313
.. function:: reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)
1414

1515
``viewname`` can be a :ref:`URL pattern name <naming-url-patterns>` or the
16-
callable view object. For example, given the following ``url``::
16+
callable view object used in the URLconf. For example, given the following
17+
``url``::
1718

1819
from news import views
1920

@@ -79,6 +80,26 @@ use for reversing. By default, the root URLconf for the current thread is used.
7980
Applying further encoding (such as :func:`urllib.parse.quote`) to the output
8081
of ``reverse()`` may produce undesirable results.
8182

83+
.. admonition:: Reversing class-based views by view object
84+
85+
The view object can also be the result of calling
86+
:meth:`~django.views.generic.base.View.as_view` if the same view object is
87+
used in the URLConf. Following the original example, the view object could
88+
be defined as:
89+
90+
.. code-block:: python
91+
:caption: ``news/views.py``
92+
93+
from django.views import View
94+
95+
96+
class ArchiveView(View): ...
97+
98+
99+
archive = ArchiveView.as_view()
100+
101+
However, remember that namespaced views cannot be reversed by view object.
102+
82103
``reverse_lazy()``
83104
==================
84105

tests/urlpatterns_reverse/tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,12 +522,12 @@ def test_illegal_kwargs_message(self):
522522
with self.assertRaisesMessage(NoReverseMatch, msg):
523523
reverse("places", kwargs={"arg1": 2})
524524

525-
def test_func_view_from_cbv(self):
525+
def test_view_func_from_cbv(self):
526526
expected = "/hello/world/"
527527
url = reverse(views.view_func_from_cbv, kwargs={"name": "world"})
528528
self.assertEqual(url, expected)
529529

530-
def test_func_view_from_cbv_no_expected_kwarg(self):
530+
def test_view_func_from_cbv_no_expected_kwarg(self):
531531
with self.assertRaises(NoReverseMatch):
532532
reverse(views.view_func_from_cbv)
533533

tests/urlpatterns_reverse/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,6 @@
137137
path("includes/", include(other_patterns)),
138138
# Security tests
139139
re_path("(.+)/security/$", empty_view, name="security"),
140-
# View function from cbv
140+
# View function from cbv.
141141
path("hello/<slug:name>/", view_func_from_cbv),
142142
]

tests/urlpatterns_reverse/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ def bad_view(request, *args, **kwargs):
5858
raise ValueError("I don't think I'm getting good value for this view")
5959

6060

61-
class _HelloView(View):
61+
class HelloView(View):
6262
def get(self, request, *args, **kwargs):
6363
return HttpResponse(f"Hello {self.kwargs['name']}")
6464

6565

66-
view_func_from_cbv = _HelloView.as_view()
66+
view_func_from_cbv = HelloView.as_view()
6767

6868
empty_view_partial = partial(empty_view, template_name="template.html")
6969
empty_view_nested_partial = partial(

0 commit comments

Comments
 (0)
0