Reputation: 2403
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>
Upvotes: 0
Views: 544
Reputation: 51292
try
{% if emp_list.has_next %}
<a href="?page={{ emp_list.next_page_number }}&choices={{ val2 }}>Next</a>
{% endif %}
Upvotes: 2
Reputation: 16806
It sounds like you've already correctly identified the reason that the next link is redirecting to the home page:
?page=2
choices
GET variable and there are no POST variables, so val2
will be ''
when it enters your long if
elif
statement.else: return HttpResponseRedirect('/employeeList')
will be executed, redirecting the user to your homepage.I don't understand when or how you would want your filter code redirecting to the homepage, but it seems there are a couple of options:
Remove the else: return HttpResponseRedirect('/employeeList')
and use different conditions when you want to redirect the user.
Find a way to prevent that else
condition from matching the next link.
For the second option, you could add the following above your else
:
elif request.GET.get('page', False):
pass
It's certainly not a pretty solution, but it seems like the quickest (and dirtiest) option without refactoring your code.
Upvotes: 2