1

As a continuation of this question Django pagination with bootstrap any my works under my Contacts app I have to ask for your help once more. I have a problem I cannot resolve. I used few suggestions about pagination but all that is changing is the bottom pagination menu. My app is still loading all 5k objects every time, over and over. I am new at this, this app is my first project and this is the final 'big' missing part. I promiss to post final cone when I'm done :) Best regards.

views.py

def index(request):

    contacts_list = contacts.objects.all()
    contacts_filter = LFilter(request.GET, queryset=contacts_list)

    paginator = Paginator(contacts_list, 50)
    try:
        page = int(request.GET.get('page','1'))
    except:
        page = 1
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        contacts = paginator.page(1)
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)


    index = contacts.number - 1 
    max_index = len(paginator.page_range)
    start_index = index - 3 if index >= 3 else 0
    end_index = index + 3 if index <= max_index - 3 else max_index
    page_range = paginator.page_range[start_index:end_index]


    return render(
        request, 'index.html',
        context={'filter': contacts_filter,
                'contacts': contacts,
                'page_range': page_range, }
                )`

index.py

{% for obj in filter.qs %}        
    Code for contact information to display               
{% endfor %}       
<div class="prev_next">
    {% if contacts.has_previous %}
        <a class="prev btn btn-info" href="?page={{contacts.previous_page_number}}">Prev</a>
    {% endif %}
    {% if contacts.has_next %}
        <a class="next btn btn-info" href="?page={{contacts.next_page_number}}">Next</a>
    {% endif %}
    <div class="pages">
        <ul>
        {% for pg in page_range %}
            {% if contacts.number == pg %}
                <li><a href="?page={{pg}}" class="btn btn-default">{{pg}}</a></li>
            {% else %}
                <li><a href="?page={{pg}}" class="btn">{{pg}}</a></li>
            {% endif %}
        {% endfor %}
        </ul>
    </div>
    <span class="clear_both"></span>
</div>
Community
  • 1
  • 1
Jan Mejor
  • 141
  • 3
  • 15
  • Surely it's contacts_filter that is the problem here, buy the pagination. – Daniel Roseman May 14 '17 at 22:49
  • So ... going like this paginator = Paginator(contacts_filter, 50) ?? I use simple django-filter app, nothing fancy there. (no solr, elastic etc. ) – Jan Mejor May 14 '17 at 23:15
  • I was just making the point that the thing that is iterating over all the contacts is presumably that contacts_filter call, not the paginator. You should use the django-debug-toolbar to make sure. – Daniel Roseman May 15 '17 at 06:17
  • True. {% for obj in filter.qs %} so there is no connection with this two sections in my template. The list and pagination menu are two separate things right now .... dar^it – Jan Mejor May 15 '17 at 08:34

0 Answers0