def order_qs(request, qs):
order = request.GET.get('order')
sort = request.GET.get('sort')
if order == 'ASC':
return qs.order_by(sort)
elif order == 'DESC':
return qs.order_by('-' + sort)
else:
return qs
def construct_page(request, qs):
""" Builds the list-view with pagination """
limit = request.GET.get('limit', 10)
page_num = request.GET.get('page')
paginator = Paginator(order_qs(request, qs), limit)
try:
page = paginator.page(page_num)
except PageNotAnInteger:
page = paginator.page(1)
except EmptyPage:
page = paginator.page(paginator.num_pages)
return page
views.py:
def category_detail_view(request, cat_slug=None):
category = get_object_or_404(Category, slug=cat_slug)
ctx = {}
ctx['category'] = category
ctx['products'] = construct_page(request, Product.objects.filter(category=category))
return render(request, 'main/category_detail.html', ctx)
Фильтрацию по такому же принципу