ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
第一步:需要安装一个第三方支持 在命令提示符中输入: ~~~ pip install django-pure-pagination ~~~ 安装好后,会显示 ~~~ C:\Users\Administrator>pip install django-pure-pagination Collecting django-pure-pagination Downloading django-pure-pagination-0.3.0.tar.gz Installing collected packages: django-pure-pagination Running setup.py install for django-pure-pagination ... done Successfully installed django-pure-pagination-0.3.0 ~~~ 第二步:在settings.py中的INSTALLED_APPS添加‘pure_pagination’,如下 ~~~ INSTALLED_APPS = ( ... 'pure_pagination', ) ~~~ 第三步:在settings.py中添加分页设置,如下 ~~~ PAGINATION_SETTINGS = { 'PAGE_RANGE_DISPLAYED': 10, 'MARGIN_PAGES_DISPLAYED': 2, 'SHOW_FIRST_PAGE_WHEN_INVALID': True, } ~~~ 具体见: https://github.com/jamespacileo/django-pure-pagination 示例代码: settings.py ~~~ …… INSTALLED_APPS = [ 'polls.apps.PollsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blogs', 'pure_pagination', ] PAGINATION_SETTINGS = { 'PAGE_RANGE_DISPLAYED': 10, 'MARGIN_PAGES_DISPLAYED': 2, 'SHOW_FIRST_PAGE_WHEN_INVALID': True, } …… ~~~ views.py ~~~ from django.shortcuts import render from .models import Banner,Article from pure_pagination import Paginator,EmptyPage,PageNotAnInteger def mylist(request): article_list=Article.objects.all() try: page = request.GET.get('page', 1) except PageNotAnInteger: page = 1 p = Paginator(article_list,per_page=1, request=request) article_list = p.page(page) ctx={ 'article_list':article_list } return render(request, 'list.html', ctx) ~~~ list.html ~~~ {% extends 'base.html' %} {% block title %}列表页{% endblock %} {% block content %} {% for v in article_list.object_list %} <p class="pad-all mar-all"> <a href="{{v.id}}">{{v.title}}</a> </p> {% endfor %} {% include "_pagination.html" %} {% endblock %} ~~~ _pagination.html > 这个页面中需要把原来的obj_list换成你页面中的article_list,不然会显示不出链接 ~~~ {% load i18n %} <div class="pagination"> <li> {% if article_list.has_previous %} <a href="?{{ article_list.previous_page_number.querystring }}" class="prev"><i class="ion-chevron-left"></i></a> {% else %} <span class="disabled prev"><i class="ion-chevron-left"></i></span> {% endif %} </li> {% for page in article_list.pages %} {% if page %} {% ifequal page article_list.number %} <li class="active"><span class="page">{{ page }}</span></li> {% else %} <li><a href="?{{ page.querystring }}" class="page">{{ page }}</a></li> {% endifequal %} {% else %} <li> ...</li> {% endif %} {% endfor %} <li> {% if article_list.has_next %} <a href="?{{ article_list.next_page_number.querystring }}" class="next"><i class="ion-chevron-right"></i></a> {% else %} <span class="disabled next"><i class="ion-chevron-right"></i></span> {% endif %} </li> </div> ~~~