1.安装

pip install django-pagination

2.修改setting.py文件

INSTALLED_APPS = [
# **
'pagination',
]

MIDDLEWARE_CLASSES = (
       # ...
       'pagination.middleware.PaginationMiddleware',
   )
TEMPLATE_CONTEXT_PROCESSORS = [
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.request',
'djangobb_forum.context_processors.forum_settings',
]

3.在要进行列表分页的页面(template)的页面上方(最好是最上面)中 导入它的tag

{% load pagination_tags %}
4.在你的模板(template)页面上,对你想要分页的列表变量(object_list)进行分页,在模板中写如下代码:(这段短代码的位置要在 放在 你显示 object_list 之前)

{% autopaginate object_list %}
5.上面对列表分页后默认每页有20个,如果你想自己自定义,可以这样:

{% autopaginate object_list 10 %}
这样对列表分页后每页显示10个。

 

PS:Django 1.9报错解决

1)AttributeError: 'WSGIRequest' object has no attribute 'REQUEST'

搜索找到 middleware.py 文件(我的在python2.7\Lib\site-packages\pagination),

把 return int(self.REQUEST['page'])
修改成 return int(self.POST['page'])

2)TypeError: sequence index must be integer, not 'slice'

搜索找到 pagination_tags.py (我的在 python2.7\Lib\site-packages\pagination\templatetags),

#first = set(page_range[:window])
#last = set(page_range[-window:])

#current = set(page_range[current_start:current_end])

修改为

first = set(list(page_range)[:window])
last = set(list(page_range)[-window:])

current = set(list(page_range)[current_start:current_end])

posted on 2016-07-24 12:29  Lorryrui  阅读(1204)  评论(0编辑  收藏  举报