16.分頁 django 自帶的分頁:django paginator 參考:https://docs.djangoproject.com/en/1.10/topics/pagination/ >>> from django.core.paginator import Paginator >>> objects = ['john', 'paul', 'george', 'ringo'] >>> p = Paginator(objects, 2) >>> p.count 4 >>> p.num_pages ##一共多少頁,也可說是最后一頁的頁數(shù) 2 >>> p.page_range [1, 2] >>> >>> page1 = p.page(1) >>> page1>>> page1.object_list ['john', 'paul'] >>> page2 = p.page(2) >>> page2.object_list ['george', 'ringo'] >>> page2.has_next() False >>> page2.has_previous() True >>> page2.has_other_pages() True >>> page2.next_page_number() Traceback (most recent call last): ... EmptyPage: That page contains no results >>> page2.previous_page_number() 1 >>> page2.start_index() # The 1-based index of the first item on this page 3 >>> page2.end_index() # The 1-based index of the last item on this page 4 >>> p.page(0) Traceback (most recent call last): ... EmptyPage: That page number is less than 1 >>> p.page(3) Traceback (most recent call last): ... EmptyPage: That page contains no results ################################### ################################### ################################### 后端處理 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.shortcuts import render def listing(request): contact_list = Contacts.objects.all() ###把內(nèi)容取出來,但不是真正取出去。 paginator = Paginator(contact_list, 25) # Show 25 contacts per page page = request.GET.get('page') ##前臺(tái)說要去那一頁,就提交到這 try: contacts = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. ##如果頁面不是一個(gè)整數(shù),交付第一頁 contacts = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. contacts = paginator.page(paginator.num_pages) ##如果取的頁數(shù)超過大頁數(shù),就返回最后一頁 return render(request, 'list.html', {'contacts': contacts}) ##把獲取到的頁面返回到前臺(tái) ################################### ################################### ################################### 前端處理 {% for contact in contacts %} {# Each "contact" is a Contact model object. #} {{ contact.full_name|upper }}
... {% endfor %} ################################### ################################### ################################### 前端處理,用bootstrap 實(shí)現(xiàn), 作業(yè):線上分頁中的前后幾頁,而不是下面的全部頁面都顯示
下面這個(gè)實(shí)例,分頁頁面按鈕數(shù)最多顯示為3個(gè)!
成都創(chuàng)新互聯(lián)公司制作網(wǎng)站網(wǎng)頁找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁設(shè)計(jì),做網(wǎng)站、網(wǎng)站設(shè)計(jì),網(wǎng)站設(shè)計(jì),企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為1000+服務(wù),成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷推廣服務(wù)!實(shí)例: 后端: def index(request): ##分頁 articles_list = models.Article.objects.all().order_by('-publish_date') ###把內(nèi)容取出來,但不是真正取出去。 paginator = Paginator(articles_list, 2) # Show 2 contacts per page page = request.GET.get('page') ##前臺(tái)說要去那一頁,就提交到這 try: articles = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. ##如果頁面不是一個(gè)整數(shù),交付第一頁 articles = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. articles = paginator.page(paginator.num_pages) ##如果取的頁數(shù)超過大頁數(shù),就返回最后一頁 page_range = range(articles.number - 1 ,articles.number + 2) max_page = paginator.num_pages return render(request,'index.html',{ 'articles': articles, 'page_range': page_range, 'max_page': max_page, }) 前端:{% if articles.has_previous %}
- previous
{% endif %} {% for p_num in page_range %} {% if 0 < p_num and p_num < max_page %} {% if articles.number == p_num %}- {{ articles.number }} {{ articles.number }}
{% else %}- {{ p_num }}
{% endif %} {% endif %} {% endfor %} {% if articles.has_next %}- next
{% endif %}
{% if articles.has_previous %}
- «
{% else %}- «
{% endif %} {% for p_num in page_range %} {% if 0 < p_num and p_num < max_page %} {% if articles.number == p_num %}- {{ articles.number }} {{ articles.number }}
{% else %}- {{ p_num }}
{% endif %} {% endif %} {% endfor %} {% if articles.has_next %}- »
{% else %}- »
{% endif %}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。