Question

I have successfully added pagination to a list_contacts view (obviously for a Contact model). And am now trying to setup the template context so that I can limit the pagination display to look like this :

    < 1 ... 5 6 7 8 9 ... 42 >

I went from this answer and tried to use a "paginator" template tag, unsuccessfully. The main problem is that I'm using ListView instead of the old object_list, and these two don't set the context the same way.

Here's my view :

from django.views.generic import ListView
from app.models import Contact

list_contacts = ListView.as_view(
        model=Contact,
        http_method_names = ['get'],
        template_name='contacts.html',
        context_object_name='contacts',
        paginate_by=6
        )

And my template "contacts.html" :

<ul class="pagination">
    {# Previous page link #}
    {% if page_obj.has_previous %}
    <li>
        <a href="?page={{ page_obj.previous }}">&laquo;</a>
    </li>
    {% else %}
    <li class="disabled">
        <a href="#">&laquo;</a>
    </li>
    {% endif %}

    {# First page #}
    {% if show_first %}
    <li>
        <a href="?page=1">1</a>
    </li>
    <li>...</li>
    {% endif %}

    {# List of pages (with current "active") #}
    {% for page in page_numbers %}
    {% ifequal page page_obj.number %}
    <li class="active">
        <a href="#">{{ page }}</a>
    </li>
    {% else %}
    <li>
        <a href="?page={{ page }}">{{ page }}</a>
    </li>
    {% endifequal %}
    {% endfor %}

    {# Last page #}
    {% if show_last %}
    <li>...</li>
    <li><a href="?page=last">{{ page_obj.pages }}</a></li>
    {% endif %}

    {# Next page link #}
    {% if page_obj.has_next %}
    <li>
        <a href="?page={{ page_obj.next_page_number }}">&raquo;</a>
    </li>
    {% else %}
    <li class="disabled">
        <a href="#">&raquo;</a>
    </li>
    {% endif %}
</ul>

EDIT: here's my view code that ended up working

class ListContactsView(ListView):
    model = Contact
    http_method_names = ['get']
    template_name = 'contacts.html'
    context_object_name = 'contacts'
    paginate_by = 6

    def get_context_data(self, **kwargs):
        _super = super(ListContactsView, self)
        context = _super.get_context_data(**kwargs)
        adjacent_pages = 2
        page_number = context['page_obj'].number
        num_pages = context['paginator'].num_pages
        startPage = max(page_number - adjacent_pages, 1)
        if startPage <= 3:
            startPage = 1
        endPage = page_number + adjacent_pages + 1
        if endPage >= num_pages - 1:
            endPage = num_pages + 1
        page_numbers = [n for n in xrange(startPage, endPage) \
                if n > 0 and n <= num_pages]
        context.update({
            'page_numbers': page_numbers,
            'show_first': 1 not in page_numbers,
            'show_last': num_pages not in page_numbers,
            })
        return context
list_contacts = ListContactsView.as_view()
Était-ce utile?

La solution

There are different ways to achieve this (or even a combination of some):

  1. Override or extend get_context_data by calling super
  2. Write a templatetag
  3. Write a template filter
  4. Override or extend the PaginationMixin

An example of the first

class MyListView(ListView):
    def get_context_data(self, **kwargs):
        context = super(MyListView, self).get_context_data(**kwargs)
        context['foo'] = 'bar'
        return context
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top