[Answered ]-How can I make pagination to work with filtering django?

1👍

the problem is inside your views.py.you can call allusername = allusername.filter(Q(nextstatus__icontains='Technical Investigation')) before calling Padignator after making all other logic and sorting try it.

@login_required()
def investigation(request):
    search_post = request.GET.get('q')
    # Code for the users to search for reception, partno, serialno, mconum, customername
    if (search_post is not None) and search_post:
        allusername = Photo.objects.filter(Q(reception__icontains=search_post) | Q(partno__icontains=search_post) | Q(
            Customername__icontains=search_post) | Q(mcoNum__icontains=search_post) | Q(
            serialno__icontains=search_post))
        if not allusername:
            allusername = Photo.objects.all().order_by("-Datetime")


    else:
        allusername = Photo.objects.all().order_by("-Datetime")

    # Sort BY:
    part = request.GET.get('sortType')
    valid_sort = ["partno", "serialno", "Customername", "mcoNum"]  # Sort the workshop data in ascending order acording to the part number, serial number, customer name and the MCO Number
    if (part is not None) and part in valid_sort:
        allusername = allusername.order_by(part)

    page = request.GET.get('page')
    allusername = allusername.filter(Q(nextstatus__icontains='Technical Investigation')) # new
    paginator = Paginator(allusername, 10)  # 1 page will only show 10 data, if more than 10 data it will move it to the next page.
    try:
        allusername = paginator.page(page)
    except PageNotAnInteger:
        allusername = paginator.page(1)
    except EmptyPage:
        allusername = paginator.page(paginator.num_pages)

    context = {'allusername': allusername, 'query': search_post, 'order_by': part}
    return render(request, 'workshop/investigation.html', context)

Leave a comment