How Can I pass a Django Date Range Search Result page by page using Paginator in views and template

0 votes
I have written a Python Django Method for searching Income Model using Date Range and I also used pagination in the Template. My problem is that once the query result is more than one page in the template, and when clicked on the next page button, instead of displaying only the remaining search result, the page rather returns all the records in the database. Someone should please help me solve this problem. Below is my code:

def SearchIncomeRange(request):

    listIncome = Income.objects.all()

    searchForm = IncomeSearchForm(request.POST or None)

    if request.method == 'POST':

        listIncome = Income.objects.filter(date__range=[searchForm['start_date'].value(),

                                searchForm['end_date'].value()

                            ]

        )

    else:

        searchForm = IncomeSearchForm()

    #listIncome = listIncome

    paginator = Paginator(listIncome, 5)

    page = request.GET.get('page')

    paged_listIncome = paginator.get_page(page)

    #Calculate total amount of Date Range Result

    total = listIncome.aggregate(total = Sum('amount')).get('total') or 0

    context = {

        'listIncome':paged_listIncome,

        'searchForm':searchForm,

        'total':total,

    }

    return render(request, 'cashier/search_income_range.html', context)
Sep 9, 2021 in Python by SUCCESSGANDE

edited 4 days ago 7 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP