分页插件叫django-pure-pagination,github地址https://github.com/jamespacileo/django-pure-pagination
安装介绍在他项目下已经介绍的很充分了
就补充几个需要注意的地方
# views.py
from django.views.generic import ListView
from pure_pagination.mixins import PaginationMixin
from my_app.models import MyModel
class MyModelListView(PaginationMixin, ListView):
# Important, this tells the ListView class we are paginating
paginate_by = 10
# Replace it for your model or use the queryset attribute instead
object = MyModel
这是对于通用类视图的情况,这个地方object = MyModel的意思我不太明白,我觉得这是传到模板里面的变量,但是我们之前都不是这样写的啊,所以我这里去掉了object = MyModel,因为我已经有了要传到模板里面的变量。
对于pagination.html这个模板
{% load i18n %}
<div class="pagination">
{% if page_obj.has_previous %}
<a href="?{{ page_obj.previous_page_number.querystring }}" class="prev">‹‹ {% trans "previous" %}</a>
{% else %}
<span class="disabled prev">‹‹ {% 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" %} ››</a>
{% else %}
<span class="disabled next">{% trans "next" %} ››</span>
{% endif %}
</div>
这里面要注意一下django的模板语言{% trans "next" %}
trans这个tag是用于翻译的,无所谓啦,把next改成下一页之类的进行本地化,他这种翻译的方法,我客户端看起来还是英文,{% load i18n %}这个部分也是加载翻译组件的。
最后在给这些html元素加上样式就有血有肉了。