django分页linaro-django-pagination
1.安装linaro
-
django
-
pagination
settings
INSTALLED_APPS = ( # ... 'linaro_django_pagination', ) MIDDLEWARE_CLASSES = ( # ... 'linaro_django_pagination.middleware.PaginationMiddleware', ) TEMPLATE_CONTEXT_PROCESSORS = ( #在1.7中这个选项是默认取消掉了,貌似。我们如何在模板中和views中去加载是很重要的问题 '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.static', 'django.core.context_processors.tz', 'django.contrib.messages.context_processors.messages', 'django.core.context_processors.request', )
views
@login_required def documents(request): project=d_category.objects.all() doc=docs.objects.all() return render_to_response('Documents/Documents.html',{'project':project,'doc':doc,'user':request.user},context_instance=RequestContext(request))
context_instance
=
RequestContext(request)比较重要
这些Processors都会被RequestContext顺序调用,往当前Context中放入一些预定义变量。例如'django.core.context_processors.auth'作用在于默认向模板传递user、messages、perms等变量,分别描述当前登录用户、当前登录用户的消息列表和当前登录用户的权限。最后一点,当使用render_to_response方法时,RequestContext应作为其第三个参数传入。这个是我在1.7中遇到的问题,如果你不使用,那么等待的是报错。
在模板中使用
{% load pagination_tags %} {% autopaginate object_list 10 %} {% for message in object_list %} <tr> <td><input type="checkbox" class="choose" value="{{ message.pk }}"></td> <td class="name {% if message.is_read %}read{% endif %}">系统消息{% if message.is_read %}(已读){% endif %}</td> <td class="time">{{ message.create_time|date:"Y-m-d" }}</td> <td><a href="{{ message.get_absolute_url }}" class="btn btn_blue">查看</a></td> </tr> {% endfor %} </table> </form> <div class="nextPage"> <br> <span>{% paginate %}</span> </div>