There is a model category:

class Category(models.Model): title = models.CharField(max_length=200, verbose_name='Название товара') slug = models.SlugField(unique=True, null=True) class Meta: verbose_name_plural='Категории' def __unicode__(self): return self.title 

Product Model:

 class Product(models.Model): name = models.CharField(max_length=200, verbose_name='Наименование товара') price = models.CharField(max_length=10, verbose_name='Цена товара') image = models.ImageField(upload_to='images/', verbose_name='Изображение товара') product = models.ForeignKey(Category, verbose_name='Выберите категорию товара') date = models.DateTimeField(auto_now_add=True, null=True) class Meta: verbose_name_plural='Товары' def __unicode__(self): return self.name 

Views.py:

 def catalog(request): args={} args['head_title'] = Head_title.objects.get() args['head'] = Head.objects.get() args['menu'] = Menu.objects.all() args['button'] = Button.objects.get() args['category'] = Category.objects.all() product_list_all = Product.objects.all().order_by('-date') paginator = Paginator(product_list_all, 9) page = request.GET.get('page') try: products = paginator.page(page) except PageNotAnInteger: products = paginator.page(1) except EmptyPage: products = paginator.page(paginator.num_pages) args['product'] = products args['akcii'] = Akcii.objects.get() args['news'] = News.objects.all().order_by('-date')[:3] args['info'] = Info.objects.all() args['sitemap_title'] = SiteMap_title.objects.get() args['sitemap'] = SiteMap.objects.all() args['underlogo'] = Underlogo.objects.get() args['underinfo'] = Underinfo.objects.get() args['form_title'] = FormTitle.objects.get() return render_to_response('catalog.html', args) 

HTML

 {% extends 'base.html' %} {% block content %} <!--section.navigation-small--> <section class="navigation-small"> <div class="container"> <div class="row"> <div class="col-md-12"> <ol class="breadcrumb"> <li><a href="{% url 'home_page' %}">Главная</a></li> <li class="active">О нас</li> </ol> </div> </div> </div> </section> <!--section.navigation-small end--> <section class="cotalog"> <div class="container"> <div class="row"> <div class="leftHandSide btn-group"> <div class="navBarL"> <div class="navHeader"> <p>Каталог</p> </div> <ul> <li><a class="filter" data-filter=".mix">Все</a></li> {% for item in category %} {% if forloop.counter == 1 %} <li><a class="filter" data-filter=".category-{{ forloop.counter }}">{{ item.title }}</a></li> {% endif %} {% if forloop.counter == 2 %} <li><a class="filter" data-filter=".category-{{ forloop.counter }}">{{ item.title }}</a></li> {% endif %} {% if forloop.counter == 3 %} <li><a class="filter" data-filter=".category-{{ forloop.counter }}">{{ item.title }}</a></li> {% endif %} {% endfor %} </ul> </div> </div> <div class="rightSide"> {% for item in product %} {% for object in category %} {% if item.product.title == object.title %} <div class="product mix category-{{ forloop.counter }}"> <img class="product_img" src="{{ item.image.url }}" width="91px" height="109px"> <p class="title">{{ item.name }}</p> <p class="price">{{ item.price }}</p> <a class="btn-cotalog">Купить</a> </div> {% endif %} {% endfor %} {% endfor %} </div> <div class="pagination"> {% if product.has_previous %} <a href="?page={{ product.previous_page_number }}">Назад</a> {% endif %} {% for num in product.paginator.page_range %} {% ifequal num product.number %} <span class="current"><b>{{ num }}</b></span> {% else %} <a href="?page={{ num }}"> {{ num }}</a> {% endifequal %} {% endfor %} {% if product.has_next %} <a href="?page={{ product.next_page_number }}">Следующая страница</a> {% endif %} </div> {% endif %} </div> </div> </section> {% include 'Main/akcii.html' %} {% include 'Main/news.html' %} {% endblock %} 

The problem is that when you click on any of the categories Pagination works for all products, and not for each category separately.

  • You href not on the category, maybe this is the problem - FeroxTL

1 answer 1

I do not see the transfer of arguments responsible for a separate category, there is only GET for pages

 def catalog(request) 

Applies to all products without categorization.

 paginator = Paginator(Product.objects.all().order_by('-date'), 9) 

Therefore, pagination works for all products regardless of category.

You must pass the category, either in get or in

 def catalog(request, cat_slug) 

And based on this already filter the goods

 paginator = Paginator(Product.objects.all().filter(product__slug=cat_slug).order_by('-date'), 9)