安装
pip install django-pure-pagination
注册应用
INSTALLED_APPS = (
'pure_pagination',
)
配置设置--settings
PAGINATION_SETTINGS = {
'PAGE_RANGE_DISPLAYED': 1,
'MARGIN_PAGES_DISPLAYED': 2,
'SHOW_FIRST_PAGE_WHEN_INVALID': True,
}
视图:
art和articles根据类改动try:
page = request.GET.get('page',1)# 接收页码
except PageNotAnInteger:# 无效的
page =1 # 返回页码为1
# jokes = Joke.objects.all()
jokes = Joke.objects.filter(isdelete=False).all()
p = Paginator(jokes,per_page=5,request=request)# per_page=5 每页五条数据
jokes = p.page(page)# 取多少页
ctx = {'jokes':'jokes'}
创建模板:_pagination.html
{% load i18n %}
<div class="pagination">
{% if articles.has_previous %}
<a href="?{{ articles.previous_page_number.querystring }}"
class="prev">上一页</a>
{% else %}
<span class="disabled prev">上一页 </span>
{% endif %}
{% for page in articles.pages %}
{% if page %}
{% ifequal page articles.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="?{{ articles.next_page_number.querystring }}" class="next">下一页</a>
{% else %}
<span class="disabled next">下一页</span>
{% endif %}
</div>
网友评论