Skip to content Skip to sidebar Skip to footer

Filter The Number Of Pages Not Working Django

I have a page where it will display the details that is in my database in a form of a table, I am doing a filtering so when the list gets very long at least it can split up into di

Solution 1:

in your views.py there is a problem of indentation and you should pass the search_post in the template

@login_required()
def ViewMCO(request):
    search_post = request.GET.get('q')
    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(status__icontains=search_post)
                                           | Q(serialno__icontains=search_post))
        if not allusername:
            allusername = Photo.objects.all().order_by("-Datetime")
        page = request.GET.get('page')
        paginator = Paginator(allusername, 10)
        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}
        return render(request, 'ViewMCO.html', context)
    else:#if search_post is None 
        return redirect('somewhere')
   

and now in your template there is work to do.
change this

 {% if allusername.has_other_pages %}
  <ul class="pagination">
    {% if allusername.has_previous %}
      <li><a href="?page={{ allusername.previous_page_number }}">&laquo;</a></li>
    {% else %}
      <li class="disabled"><span>&laquo;</span></li>
    {% endif %}
    {% for i in allusername.paginator.page_range %}
      {% if allusername.number == i %}
        <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
      {% else %}
        <li><a href="?page={{ i }}">{{ i }}</a></li>
      {% endif %}
    {% endfor %}
    {% if allusername.has_next %}
      <li><a href="?page={{ allusername.next_page_number }}">&raquo;</a></li>
    {% else %}
      <li class="disabled"><span>&raquo;</span></li>
    {% endif %}
    </ul>
    {% endif %}

to

 {% if allusername.has_other_pages %}
  <ul class="pagination">
    {% if allusername.has_previous %}
      <li><a href="?q={{ query|urlencode }}&page={{ allusername.previous_page_number }}">&laquo;</a></li>
    {% else %}
      <li class="disabled"><span>&laquo;</span></li>
    {% endif %}
    {% for i in allusername.paginator.page_range %}
      {% if allusername.number == i %}
        <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
      {% else %}
        <li><a href="?q={{ query|urlencode }}&page={{ i }}">{{ i }}</a></li>
      {% endif %}
    {% endfor %}
    {% if allusername.has_next %}
      <li><a href="?q={{ query|urlencode }}&page={{ allusername.next_page_number }}">&raquo;</a></li>
    {% else %}
      <li class="disabled"><span>&raquo;</span></li>
    {% endif %}
    </ul>
    {% endif %}

what i change in the pagination is that i passed the 'query' in the template and i use urlencode.


Post a Comment for "Filter The Number Of Pages Not Working Django"