Python Django-没有与给定查询匹配的模型
我正在显示一个包含产品的目录,这些产品是按类别筛选的,但是当尝试从模板中的链接调用ListView时,它返回没有与给定查询匹配的类别 观点:Python Django-没有与给定查询匹配的模型,python,django,django-models,django-views,django-templates,Python,Django,Django Models,Django Views,Django Templates,我正在显示一个包含产品的目录,这些产品是按类别筛选的,但是当尝试从模板中的链接调用ListView时,它返回没有与给定查询匹配的类别 观点: class CategoryProductList(ListView): template_name = 'catalog/product/product_list.html' def get_queryset(self): self.category = get_object_or_404(Category, name=s
class CategoryProductList(ListView):
template_name = 'catalog/product/product_list.html'
def get_queryset(self):
self.category = get_object_or_404(Category, name=self.kwargs['category_slug'])
return Product.objects.filter(category=self.category)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['categories'] = Category.objects.all()
context['category'] = self.category
context['category_slug'] = self.category.slug
return context
url模式:
path('catalog/<slug:category_slug>/list/', views.CategoryProductList.as_view(), name='category_product_list_table'),
path('catalog//list/',views.CategoryProductList.as_view(),name='category_product_list_table'),
调用ListView的按钮:
<li class="nav-item">
<a href="{% if category %}{% url 'catalog:category_product_list_table' category.slug %}{% else %}{% url 'catalog:product_list_table' %}{% endif %}" class="btn btn-secondary btn-sm">
<i class="uil-list-ul"></i>
View as table
</a>
</li>
我做错了什么?请将完整的错误回溯添加到您的问题中。没有错误回溯。它只是显示一个页面,上面写着“没有类别匹配给定的查询”。这是页面上唯一的内容吗?“航站楼里什么都没有?”克拉姆说。终端显示以下内容:
[07/Nov/2020 02:34:50]“GET/catalog/pucked strings/list/HTTP/1.1”404 1784
您刚刚键入了{%url'catalog:product\u list\u table%}
,但如果没有分类,它将无法工作。请将完整的错误回溯添加到您的问题中。没有错误回溯。它只是显示一个页面,上面写着“没有类别匹配给定的查询”。这是页面上唯一的内容吗?“航站楼里什么都没有?”克拉姆说。终端显示以下内容:[07/Nov/2020 02:34:50]“GET/catalog/pucked strings/list/HTTP/1.1”404 1784
您刚刚键入了{%url'catalog:product\u list\u table%}
,但是没有类别它就不能工作