I have created a Django application. There is a filtering functionality in my application. The filtered data is shown in a separate page.
Now I want to give Django Pagination to the filtered list page. While doing it, I came into this error. The pagination is shown at the bottom of the page with showing the correct number of pages NEXT.
But while clicking on the 'Next Page' in paginator it redirects me to my home page (/employeeList).
I suppose it is because of an HttpResponseRedirect (return HttpResponseRedirect('/employeeList'))
I give inside the filter()
which is used in some cases. I will paste my filter() and the HTML for pagination.
def filter(request):
val2=''
val3=''
newData=''
if request.GET.has_key('choices'):
val2=request.GET.get('choices')
if request.GET.has_key('textField'):
val3=request.GET.get('textField')
if request.POST:
val2=request.POST.get('choices')
val3=request.POST.get('textField')
if val2=='Designation':
newData = EmployeeDetails.objects.filter(designation=val3)
flag=True
elif val2=='Name':
newData = EmployeeDetails.objects.filter(userName__icontains=val3)
flag=True
elif val2=='EmployeeID':
newData = EmployeeDetails.objects.filter(employeeID=val3)
flag=True
elif val2=='Project':
newData = EmployeeDetails.objects.filter(project=val3)
flag=True
elif val2=='DateOfJoin':
newData = EmployeeDetails.objects.filter(dateOfJoin=val3)
flag=True
else:
return HttpResponseRedirect('/employeeList')
if request.POST.get('sortAscendValue'):
newData = newData.order_by('userName')
elif request.POST.get('sortDescendValue'):
newData = newData.order_by('-userName')
paginator = Paginator(newData, 10)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(0)
return render_to_response('filter.html',{'newData':newData,'emp_list': contacts,'val2':val2,'val3':val3,'flag':flag,'emp_list': contacts})
filter.html
<div class="pagination" >
<span class="step-links">
{% if emp_list.has_previous %}
<a href="?page={{ empl_list.previous_page_numbe }}">Previous</a>
{% endif %}
<span class="current">
Page {{ emp_list.number }} of {{ emp_list.paginator.num_pages }}.
</span>
{% if emp_list.has_next %}
<a href="?page={{ emp_list.next_page_number }}">Next</a>
{% endif %}
</span>
</div>