Делаю тесты на соответствие страницы запроса и функции представления.
С функциями разобрался и все работает.
urls:
from django.urls import path
from private_site.views import *
urlpatterns = [
path('', index, name='home'),
path('register/', RegisterUser.as_view(), name='register'),
path('login/', LoginUser.as_view(), name='login'),
path('logout/', logout_user, name='logout'),
path('content/', content, name='content'),
]
views:
def index(request):
return render(request, "private_site/index.html", {'title': "Главная страница"})
class RegisterUser(CreateView):
form_class = RegisterUserForm
template_name = 'private_site/register.html'
success_url = reverse_lazy('login')
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "Регистрация"
return context
def form_valid(self, form):
user = form.save()
login(self.request, user)
return redirect('content')
Все работает когда функция представления
test:
class HomePageTest(TestCase):
def test_url_to_home_page_views(self):
found = resolve('/')
self.assertEqual(found.func, index)
С классом представления не работает как я его только не вызывал
class RegisterPageTest(TestCase):
def test_url_to_register_page_views(self):
found = resolve('/register/')
print(found)
self.assertEqual(found.func, RegisterUser.as_view)
Creating test database for alias ‘default’…
System check identified no issues (0 silenced).
..ResolverMatch(func=private_site.views.RegisterUser, args), kwargs={}, url_name='register', app_names=, names
paces=, route='register/')
F
======================================================================
FAIL: test_url_to_register_page_views (private_site.tests.test_views.RegisterPageTest)
———————————————————————-
File “E:\my_site_django\work_site\private_site\tests\test_views.py”, line 31, in test_url_to_register_page_views
self.assertEqual(found.func, RegisterUser.as_view)
AssertionError: <function View.as_view.<locals>.view at 0x000001CE703A2CB0> != <bound method View.as_view of <clas
s ‘private_site.views.RegisterUser’>>
———————————————————————-
Ran 3 tests in 0.025s
FAILED (failures=1)