Как отдебажить получаемый шаблон в Django
Не понимаю, почему не проходит тест шаблона. Падает ошибка: AssertionError: No templates used to render the response.
Содержимое объекта response из которого видно что в нем уже есть шаблон products/index.html:
Код /store/products/tests.py:
from django.test import TestCase
from django.urls import reverse
import django
django.setup()
class IndexViewTestCase(TestCase):
def test_view(self):
path = reverse('index')
response = self.client.get(path)
print(response.template_name)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context_data['title'], 'Store')
self.assertTemplateUsed(response, 'products/index.html')
Здесь использую import django и django.setup() потому что падала ошибка:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Django требовал, чтобы testserver был добавлен в ALLOWED_HOSTS, что я и сделал:
ALLOWED_HOSTS = ['127.0.0.1', 'testserver']
Код /store/products/views.py:
class IndexView(TitleMixin, TemplateView):
template_name = 'products/index.html'
title = 'Store'
class ProductsListView(TitleMixin, ListView):
model = Product
template_name = 'products/products.html'
paginate_by = 3
title = 'Store - каталог'
def get_queryset(self):
queryset = super(ProductsListView, self).get_queryset()
category_id = self.kwargs.get('category_id')
return queryset.filter(category_id=category_id) if category_id else queryset
def get_context_data(self, *, object_list=None, **kwargs):
context = super(ProductsListView, self).get_context_data()
context['categories'] = ProductCategory.objects.all()
return context
/store/products/urls.py:
from django.urls import path
from products.views import ProductsListView, basket_add, basket_remove
app_name = 'products'
urlpatterns = [
path('', ProductsListView.as_view(), name='index'),
path('category/<int:category_id>/', ProductsListView.as_view(), name='category'),
path('page/<int:page>/', ProductsListView.as_view(), name='paginator'),
path('baskets/add/<int:product_id>/', basket_add, name='basket_add'),
path('baskets/remove/<int:basket_id>/', basket_remove, name='basket_remove'),
]
Конфиг pytest:
Пробовал запуск не pytest'ом, а обычной python-конфигурацией. Здесь тесты проходят:

В этом случае конфиг был такой:

Пробовал использовать 2 варианта написания reverse чтобы явно указать, что хочу получить путь к представлению index в приложении products но не помог ни один из них:
reverse('products:index')
reverse('products:index', current_app='products')
Может быть дело в том, что в проекте существуют другие URL-шаблоны, которые имеют имя index и отличаются от пути, который соответствует представлению IndexView? Есть корневой urls, который тоже содержит index:
urlpatterns = [
path('', IndexView.as_view(), name='index'),
path('accounts/', include('allauth.urls')),
path('admin/', admin.site.urls),
path('password_reset/', ResetPasswordView.as_view(), name='password_reset'),
path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('products/', include('products.urls', namespace='products')),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
path('users/', include('users.urls', namespace='users')),
path('web/', include('web.urls')),
]

