# 7.3.列表分页

django-pure-pagination

用法：<https://github.com/jamespacileo/django-pure-pagination>

## 安装

安装django-pure-pagination

```bash
pip install django-pure-pagination
```

在INSTALLED\_APPS中添加 pure\_pagination

```python
INSTALLED_APPS = (
    ...
    'pure_pagination',
)
```

最后，使用**from pure\_pagination import Paginator**替换 **from django.core.paginator import Paginator**

## 设置

A few settings can be set within settings.py

```python
PAGINATION_SETTINGS = {
    'PAGE_RANGE_DISPLAYED': 10,
    'MARGIN_PAGES_DISPLAYED': 2,

    'SHOW_FIRST_PAGE_WHEN_INVALID': True,
}
```

**PAGE\_RANGE\_DISPLAYED** is the number of pages neighbouring the current page which will be displayed (default is 10)

**MARGIN\_PAGES\_DISPLAYED** is the number of pages neighbouring the first and last page which will be displayed (default is 2)

Set **SHOW\_FIRST\_PAGE\_WHEN\_INVALID** to True when you want to just show first page when provided invalid page instead of 404 error

## Usage example

Following is a simple example for **function based views**. For generic class-based views, see bellow.

view file: **views.py**

```python
# views.py
from django.shortcuts import render_to_response

from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

def index(request):

    objects = ['john', 'edward', 'josh', 'frank']
    try:
        page = request.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    p = Paginator(objects, 5, request=request)
    people = p.page(page)
    return render_to_response('index.html', {
        'people': people,
    }
```

## Usage

There a few different way you can make use of the features introduced within django-pure-pagination.

Easiest way to render the pagination is to call the render method i.e.&#x20;

Alternatively you can access the Page object low level methods yourself

**Special note:** **page\_obj** and **current\_page** both point to the page object within the template.

```markup
{% load i18n %}
<div class="pagination">
    {% if page_obj.has_previous %}
        <a href="?{{ page_obj.previous_page_number.querystring }}" class="prev">&lsaquo;&lsaquo; {% trans "previous" %}</a>
    {% else %}
        <span class="disabled prev">&lsaquo;&lsaquo; {% trans "previous" %}</span>
    {% endif %}
    {% for page in page_obj.pages %}
        {% if page %}
            {% ifequal page page_obj.number %}
                <span class="current page">{{ page }}</span>
            {% else %}
                <a href="?{{ page.querystring }}" class="page">{{ page }}</a>
            {% endifequal %}
        {% else %}
            ...
        {% endif %}
    {% endfor %}
    {% if page_obj.has_next %}
        <a href="?{{ page_obj.next_page_number.querystring }}" class="next">{% trans "next" %} &rsaquo;&rsaquo;</a>
    {% else %}
        <span class="disabled next">{% trans "next" %} &rsaquo;&rsaquo;</span>
    {% endif %}
</div>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://moluo.gitbook.io/notes/bian-cheng-xue-xi/python/django/7.3.-lie-biao-fen-ye.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
