我的产品按类别排序,我想对每个类别的产品进行分页和按钮"显示所有"显示所选类别的所有产品。 但是当我点击"显示全部"我从第一类获得产品。
产品/ 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 %}
答案 0 :(得分:0)
我很惊讶下面的代码没有抛出错误
products = Product.objects.active(category__id=context['categories'])
但我认为要实现您的目标,您应该将context['products']
的返回值更改为列表,并将查询集从__id
更改为__in
,然后再次
products = Product.objects.active(category__in=context['categories'].values_list('id', flat=True))
答案 1 :(得分:0)
此变体采用了正确的类别。
我只是写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'])