0
0
mirror of https://github.com/django/django.git synced 2024-11-24 20:07:01 +01:00

Refs #28999 -- Added tests for reversing a class-based view by instance.

This commit is contained in:
Clifford Gama 2024-10-17 17:59:58 +02:00 committed by Sarah Boyce
parent 35ab2e0182
commit be138f32ed
3 changed files with 20 additions and 1 deletions

View File

@ -522,6 +522,15 @@ class URLPatternReverse(SimpleTestCase):
with self.assertRaisesMessage(NoReverseMatch, msg):
reverse("places", kwargs={"arg1": 2})
def test_func_view_from_cbv(self):
expected = "/hello/world/"
url = reverse(views.view_func_from_cbv, kwargs={"name": "world"})
self.assertEqual(url, expected)
def test_func_view_from_cbv_no_expected_kwarg(self):
with self.assertRaises(NoReverseMatch):
reverse(views.view_func_from_cbv)
class ResolverTests(SimpleTestCase):
def test_resolver_repr(self):

View File

@ -8,6 +8,7 @@ from .views import (
empty_view_partial,
empty_view_wrapped,
nested_view,
view_func_from_cbv,
)
other_patterns = [
@ -136,4 +137,6 @@ urlpatterns = [
path("includes/", include(other_patterns)),
# Security tests
re_path("(.+)/security/$", empty_view, name="security"),
# View function from cbv
path("hello/<slug:name>/", view_func_from_cbv),
]

View File

@ -3,7 +3,7 @@ from functools import partial, update_wrapper
from django.contrib.auth.decorators import user_passes_test
from django.http import HttpResponse
from django.urls import reverse_lazy
from django.views.generic import RedirectView
from django.views.generic import RedirectView, View
def empty_view(request, *args, **kwargs):
@ -58,6 +58,13 @@ def bad_view(request, *args, **kwargs):
raise ValueError("I don't think I'm getting good value for this view")
class _HelloView(View):
def get(self, request, *args, **kwargs):
return HttpResponse(f"Hello {self.kwargs['name']}")
view_func_from_cbv = _HelloView.as_view()
empty_view_partial = partial(empty_view, template_name="template.html")
empty_view_nested_partial = partial(
empty_view_partial, template_name="nested_partial.html"