Python Don';t工作“;“全部显示”;滤波器
我有按类别分类的产品,我想对每个类别的产品进行分页,并按“全部显示”按钮显示所选类别中的所有产品。 但当我单击“全部显示”时,我会从第一类中获得产品。 产品/视图.py category.htmlPython Don';t工作“;“全部显示”;滤波器,python,django,pagination,Python,Django,Pagination,我有按类别分类的产品,我想对每个类别的产品进行分页,并按“全部显示”按钮显示所选类别中的所有产品。 但当我单击“全部显示”时,我会从第一类中获得产品。 产品/视图.py category.html {%if products.paginator%} {%if products.has_previous%} {%endif%} 我很惊讶下面的代码没有抛出错误 products = Product.objects.active(category__id=context['categories'])
{%if products.paginator%}
{%if products.has_previous%}
{%endif%}
我很惊讶下面的代码没有抛出错误
products = Product.objects.active(category__id=context['categories'])
但是我假设,为了实现您想要的,您应该将上下文['products']
的返回更改为列表,并将查询集从\uu id
更改为\uu in
等等
products = Product.objects.active(category__in=context['categories'].values_list('id', flat=True))
这种变体属于正确的类别 我只是写
Product.objects.active(category=context['category'])
而不是products=
Product.objects.active(category\uu id=context['categories'])
您可以显示
.active
modelmanager吗?请删除此代码!排序是在数据库级别完成的。而不是检索整个表并在django中执行。过滤也在后端完成。了解django关系。最好还是从学习数据库关系开始。@DeanChristianArmadaclass ProductManager(models.Manager):def active(self,**kwargs):return self.filter(active=True,**kwargs)。order_by('sort_order')
products = Product.objects.active(category__id=context['categories'])
products = Product.objects.active(category__in=context['categories'].values_list('id', flat=True))
show_all_products = self.request.GET.get('show')
if show_all_products == 'all':
products = Product.objects.active(category=context['category'])