如何在django中的函数基视图中实现分页

如何在django中的函数基视图中实现分页,django,django-views,django-pagination,Django,Django Views,Django Pagination,我知道如何使用ListView class ProductListView(ListView): model = product template_name = 'products/product_list.html' context_object_name = 'products' ordering = ['-pub_date'] paginate_by = 5 但我不知道如何在函数基视图中实现分页。我读到我们必须为d

我知道如何使用
ListView

class ProductListView(ListView):
       model = product
       template_name = 'products/product_list.html'  
       context_object_name = 'products'
       ordering = ['-pub_date']
       paginate_by = 5
但我不知道如何在函数基视图中实现分页。我读到我们必须为
django.core.Paginator导入Paginator
,并在函数基视图中使用它,如
Paginator=Paginator(qs,5)
,但它不起作用

    def Productlistview(request):
       qs = product.objects.all()
       location_search = request.GET.get('location')
       print(location_search)
       categories = request.GET.get('categories')
       price = request.GET.get('price')
       ordering = ['-pub_date']
       paginator = Paginator(qs, 5)
       if location_search != "" and location_search is not None:
              qs = qs.filter(location__contains=location_search)

     context = {
       'posts': qs
      }

    return render(request, "products/product_list.html", context)
这在本手册中有介绍。您确实使用了分页器,但仍然需要传递要显示的页面。这通常是通过GET参数完成的,例如
page
。此外,您应该在分页之前进行筛选和排序

def product_list_view(request):
    qs = product.objects.all()
    location_search = request.GET.get('location')
    print(location_search)
    categories = request.GET.get('categories')
    price = request.GET.get('price')
    if location_search:
        qs = qs.filter(location__contains=location_search)
    qs = qs.order_by('-pub_date')
    paginator = Paginator(qs, 5)
    page = p.page(request.GET.get('page'))
     context = {
       'posts': page
      }

    return render(request, "products/product_list.html", context)
def产品列表视图(请求):
qs=product.objects.all()
location\u search=request.GET.GET('location')
打印(位置搜索)
categories=request.GET.GET('categories')
price=request.GET.GET('price')
如果位置搜索:
qs=qs.filter(位置\包含=位置\搜索)
qs=qs.order(发布日期)
paginator=paginator(qs,5)
page=p.page(request.GET.GET('page'))
上下文={
“帖子”:第页
}
返回渲染(请求“products/product_list.html”,上下文)