简体   繁体   中英

How to implement pagination in Search results in django?

I found a resource where it has code like this:

from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def index(request):
    user_list = User.objects.all()
    page = request.GET.get('page', 1)

    paginator = Paginator(user_list, 10)
    try:
        users = paginator.page(page)
    except PageNotAnInteger:
        users = paginator.page(1)
    except EmptyPage:
        users = paginator.page(paginator.num_pages)

    return render(request, 'core/user_list.html', { 'users': users })

I have a code like this:

def product_list(request, category_slug=None):

    category = None
    categories = Category.objects.all().order_by("-rating")
    #paginator = Paginator(categories, 10)
    products = Product.objects.all().order_by("-number")
    users = User.objects.exclude(id=request.user.id)
    query = request.GET.get('q')
    if query=='':
        return HttpResponseRedirect('/')
    if query:
        categories = Category.objects.filter(Q(slug__icontains=query)| Q(url__icontains=query)).order_by("-rating") 
        products = Product.objects.filter(Q(slug__icontains=query) | Q(name__icontains=query) | Q(description__icontains=query)).order_by("number")
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        products = Product.objects.filter(category=category)

    categories_counter = products.annotate(Count('id'))
    categories_count = len(categories_counter)
    #contacts = paginator.get_page(query)

    context = {
        'category': category,
        'categories': categories,
        'products': products,
        'categories_count':categories_count,
        'query':query,
        'users':users,
        #'contacts':contacts,
    }
    return render(request, 'shop/product/list.html', context)

I have retrieved objects from two models, Category, and Product. How do I implement the pagination code in this view? It's not a normal pagination but pagination in search results.

user_list = User.objects.all() # this is the full queryset, contains all objects
page = request.GET.get('page', 1) # this is the page number of page whose data you want to retrieve, you need to pass page value as query params

paginator = Paginator(user_list, 10) # this will paginate the full queryset in pages of 10 objects
try:
    users = paginator.page(page) # this will return data of that particular page
except PageNotAnInteger:
    users = paginator.page(1) # if the passed page is not Integer then first page is returned, you can customize this
except EmptyPage:
    users = paginator.page(paginator.num_pages) # if that page contains no element, then last page is returned, you can customize this also

You can apply same logic to retrieve categories in category_page and other data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM