Django: pagination in general views?

This is in my urls.py:

group_info = {
    'queryset': Group.objects.all(),
    'template_object_name': 'groups',
    'paginate_by': 25,
}

And this is the relevant URL: (r '^ groups / $', 'django.views.generic.list_detail.object_list', group_info),

And this is my code in the template:

<div class="pagination">
    <span class="step-links">
        {% if groups.has_previous %}
            <a href="?page={{ groups.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ groups.number }} of {{ groups.paginator.num_pages }}.
        </span>

        {% if groups.has_next %}
            <a href="?page={{ groups.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

.. but the page information is not displayed. I think that I am doing this in the same way as the documentation does. Any idea what is wrong?

Thank.

+3
source share
1 answer

You are using the wrong variable names. As docs say , variable names paginatorfor the paginator object and page_objfor the page.

{% if is_paginated %}
    <div class="pagination">
        <span class="step-links">
            {% if page_obj.has_previous %}
                <a href="?page={{ page_obj.previous_page_number }}">previous</a>
            {% endif %}

            <span class="current">
                Page {{ page_obj.number }} of {{ paginator.num_pages }}.
            </span>

            {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}">next</a>
            {% endif %}
        </span>
    </div>
{% endif %}
+17
source

Source: https://habr.com/ru/post/1726716/


All Articles