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)