1. 将该APP安装至Django项目中。(settings.py)
INSTALLED_APPS = ( # ... ‘pagination‘, )
2. 在Django项目的middleware中安装此APP:(请修改settings.py)
MIDDLEWARE_CLASSES = ( # ... ‘pagination.middleware.PaginationMiddleware‘, )
3. 添加context上下文(settings.py)
TEMPLATE_CONTEXT_PROCESSORS = ( # ‘django.core.context_processors.auth‘, ‘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‘, )
4.导入tag并在你的模板(template)页面上,对你想要分页的列表变量(object_list)进行分页
{% load pagination_tags %}
{% autopaginate object_list 15 %} {% for object in object_list %} {% object.name %} {% endfor %} {% paginate %}
7、views文件
from django.template import RequestContext
def test(request): contacts = Blog.objects.all() return render_to_response(‘test.html‘,{‘object_list‘:contacts},context_instance=RequestContext(request))
时间: 2024-10-28 11:35:06