Google Code的Project可能被作者关闭了,现在没法打开,具体原因未知。
解压、安装。
完成后配置如下:
1. 将安装文件中的 pagination 文件夹拷贝到项目的根目录下
2. settings.py 修改
添加 App
- INSTALLED_APPS = (
- # ...
- 'pagination',
- )
- INSTALLED_APPS = (
- # ...
- 'pagination',
- )
添加中间件
- MIDDLEWARE_CLASSES = (
- # ...
- 'pagination.middleware.PaginationMiddleware',
- )
- MIDDLEWARE_CLASSES = (
- # ...
- 'pagination.middleware.PaginationMiddleware',
- )
添加
- TEMPLATE_CONTEXT_PROCESSORS = (
- "django.core.context_processors.auth",
- "django.core.context_processors.debug",
- "django.core.context_processors.i18n",
- "django.core.context_processors.media",
- "django.core.context_processors.request"
- )
- TEMPLATE_CONTEXT_PROCESSORS = (
- "django.core.context_processors.auth",
- "django.core.context_processors.debug",
- "django.core.context_processors.i18n",
- "django.core.context_processors.media",
- "django.core.context_processors.request"
- )
在模板中顶部位置,添加{% load pagination_tags %};
添加分页处理,默认为每 20 项分页一次:{% autopaginate contents %},其中 contents即为 views 所传递的结果集;如果需要按每 10 项分页一次,可写为:{% autopaginate contents 10 %};
展现分页结果,在模板中,内容呈现的后面输入:{% paginate %}
4. views.py 编写
views.py 中的内容比使用Django自带分页机制所写要简单很多,内容如下:
- def listing(request):
- contents = Contents.objects.all()
- return render_to_response('templates_tmp.html', {"contents": contents})
- def listing(request):
- contents = Contents.objects.all()
- return render_to_response('templates_tmp.html', {"contents": contents})
5. 其他选项
- PAGINATION_DEFAULT_PAGINATION 每页显示数量
- PAGINATION_DEFAULT_WINDOW 分页显示在当前页左右两边的页数
- PAGINATION_DEFAULT_ORPHANS 最后一页显示的最小页数,默认为0
- PAGINATION_INVALID_PAGE_RAISES_404 当页数不存在时,是否显示404页面
转自 http://blog.youkuaiyun.com/l_courser/article/details/7038859