不工作"show all"过滤器



我有按类别排序的产品,我想对每个类别和按钮的产品进行分页"显示所有"显示"类别中所有产品的所有产品。但是,当我单击"显示"时,我会从第一类获取产品。

product/views.py

class CategoryView(DetailView):
    model = Category
    template_name = 'shop/product/category.html'
    context_object_name = 'category'
    slug_url_kwarg = 'category_slug'
    def get_context_data(self, **kwargs):
        context = super(CategoryView, self).get_context_data(**kwargs)
        context['category'] = get_object_or_404(Category, slug=self.kwargs['category_slug'])
        context['categories'] = Category.objects.active()
        context['products'] = Product.objects.active(category=context['category'])
        context['brands'] = Brand.objects.filter(product__in=context['products']).distinct()
        context['weight'] = filter(None, sorted(list(set(list(p.weight if p.weight is not None else None for p in
                                                              context['products'])))))
        context['package_type'] = filter(None, sorted(list(set(list(p.package_type if p.package_type is not None else
                                                                    None for p in context['products'])))))
        context['color_type'] = filter(None, sorted(list(set(list(p.color_type if p.color_type is not None else None
                                                                  for p in context['products'])))))
        product_filter = {}
        context['product_filter'] = product_filter
        if 'filter' in self.request.resolver_match.kwargs:
            filters = self.request.resolver_match.kwargs['filter'].split(";")
            for f in filters:
                if "brand_" in f:
                    product_filter['brand'] = [x for x in f.split("brand_")[1].split(',')]
                    context['products'] = context['products'].filter(brand__slug__in=product_filter['brand'])
                if "weight" in f:
                    product_filter['weight'] = [str(x) for x in f.split("weight_")[1].split(',')]
                    context['products'] = context['products'].filter(weight__in=product_filter['weight'])
                if "package_type" in f:
                    product_filter['package_type'] = [str(x) for x in f.split("package_type_")[1].split(',')]
                    context['products'] = context['products'].filter(package_type__in=product_filter['package_type'])
                if "color_type" in f:
                    product_filter['color_type'] = [str(x) for x in f.split("color_type_")[1].split(',')]
                    context['products'] = context['products'].filter(color_type__in=product_filter['color_type'])
        show_all_products = self.request.GET.get('show')
        if show_all_products == 'all':
            products = Product.objects.active(category__id=context['categories'])
            print (context['categories'])
        else:
            paginate = 3
            products_per_page = getattr(settings, 'PRODUCTS_IN_CATEGORY_PER_PAGE', paginate)
            paginator = Paginator(context['products'], products_per_page)
            page = self.request.GET.get('page')
            try:
                products = paginator.page(page)
            except PageNotAnInteger:
                products = paginator.page(1)
            except EmptyPage:
                products = paginator.page(paginator.num_pages)
        context['products'] = products
        return context

category.html

{% if products.paginator %}
        <div class="pagin">
            <a href={% url "shop_product_category_view" category_slug=category.slug  %}?show=all   class="all">{% trans 'Показать все' %}</a>
            {% if products.has_previous %}
                <a href="?page={{ products.previous_page_number }}" class="nav_buttons">
                    <span class="icon icon-necessary_to_know-nav-left"></span>
                    {% trans 'Пред' %}
                </a>
            {% endif %}

我很惊讶下面的代码没有扔错误

products = Product.objects.active(category__id=context['categories'])

,但我认为要实现您想要的目标,您应该将context['products']的返回更改为列表,然后将QuerySet从__id更改为__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__id=context['categories'])

 show_all_products = self.request.GET.get('show')
    if show_all_products == 'all':
        products = Product.objects.active(category=context['category']) 

最新更新