2017-05-02 3 views
0

Ich habe Produkte nach Kategorien sortiert und ich möchte Seitenumbruch für Produkte jeder Kategorie und Schaltfläche "Alle anzeigen", die alle Produkte in der gewählten Kategorie zeigen. Aber wenn ich auf "Alle anzeigen" klicke, bekomme ich Produkte aus der ersten Kategorie.Funktioniert nicht "zeige alle" Filter

Produkt/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

Können Sie die '.active' ModelManger zeigen? –

+1

Geben Sie diesen Code ab! Die Sortierung erfolgt auf Datenbankebene. Nicht indem man den ganzen Tisch abholt und es im Django macht. Die Filterung erfolgt ebenfalls im Backend. Erfahren Sie mehr über Django-Beziehungen. Besser noch mit dem Lernen über Datenbankbeziehungen beginnen. – e4c5

+0

@DeanChristianArmada 'Klasse ProductManager (models.Manager): def aktiv (selbst, ** kwargs): return self.filter (aktiv = True, ** kwargs) .order_by ('sort_order') ' –

Antwort

0

Ich bin überrascht, dass der folgende Code nicht werfen einen Fehler hat

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

Aber ich nehme an, das zu erreichen, was Sie wollen, sollten Sie die Rückkehr von context['products'] auf eine Liste ändern und Ihre queryset __id-__in und wie

products = Product.objects.active(category__in=context['categories'].values_list('id', flat=True)) 
0

Diese Variante die richtige Kategorie nahm ändern.

Ich schreibe nur Product.objects.active(category=context['category']) statt 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'])