本篇文章為大家展示了使用Django框架如何實(shí)現(xiàn)一個(gè)分頁功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
導(dǎo)入分頁模塊并修改views
#只需修改index函數(shù)即可 from django.core.paginator import Paginator def index(request): messages = models.Message.objects.all() #獲取全部數(shù)據(jù) limit = 10 paginator = Paginator(messages, limit) #按每頁10條分頁 page = request.GET.get('page','1') #默認(rèn)跳轉(zhuǎn)到第一頁 result = paginator.page(page) return render(request, 'guestbook/index.html', {'messages' : result})
修改html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>留言板</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" crossorigin="anonymous"> </head> <body> <table class="table table-striped table-bordered table-hover table-condensed"> <thead> <tr class="danger"> <th>留言時(shí)間</th> <th>留言者</th> <th>標(biāo)題</th> <th>內(nèi)容</th> </tr> </thead> <tbody> {% if messages %} {% for message in messages %} <tr class="{% cycle 'active' 'success' 'warning' 'info' %}"> <td>{{ message.publish|date:'Y-m-d H:i:s' }}</td> <td>{{ message.username }}</td> <td>{{ message.title }}</td> <td>{{ message.content }}</td> </tr> {% endfor %} {% else %} <tr> <td colspan="4">無數(shù)據(jù)</td> </tr> {% endif %} </tbody> </table> <!-- 分頁開始 --> <div> <ul class="pagination"> <li><a href="/guestbook/index/?page=1" rel="external nofollow" >首頁</a></li> {% if messages.has_previous %} <li><a href="/guestbook/index/?page={{ messages.previous_page_number }}" rel="external nofollow" >上一頁</a></li> {% endif %} {% for num in messages.paginator.page_range %} <li><a href="/guestbook/index/?page={{ num }}" rel="external nofollow" >{{ num }}</a></li> {% endfor %} {% if messages.has_next %} <li><a href="/guestbook/index/?page={{ messages.next_page_number }}" rel="external nofollow" >下一頁</a></li> {% endif %} <li><a href="/guestbook/index/?page={{ messages.paginator.num_pages }}" rel="external nofollow" >尾頁</a></li> </ul> </div> <!-- 分頁結(jié)束 --> <div> <a class="btn btn-xs btn-primary" href="/guestbook/create/" rel="external nofollow" >去留言</a> </div> </body> </html>
上述內(nèi)容就是使用Django框架如何實(shí)現(xiàn)一個(gè)分頁功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道。
另外有需要云服務(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)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前題目:使用Django框架如何實(shí)現(xiàn)一個(gè)分頁功能-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://chinadenli.net/article0/ddihoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、標(biāo)簽優(yōu)化、云服務(wù)器、做網(wǎng)站、動(dòng)態(tài)網(wǎng)站、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容