溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

django實(shí)現(xiàn)分頁(yè)(pure-pagination)

發(fā)布時(shí)間:2020-07-17 06:49:15 來(lái)源:網(wǎng)絡(luò) 閱讀:1023 作者:MonCheri_Flower 欄目:開發(fā)技術(shù)

安裝

pip install django-pure-pagination
具體步驟搜索GitHub里面詳情。
class OrgView(View):
    """
        課程機(jī)構(gòu)列表功能
    """

    def get(self, request):
    
        all_orgs = CourseOrg.objects.all()  # 課程機(jī)構(gòu)
        org_nums = all_orgs.count()  # 記錄多少家
        # 城市
        all_citys = CityDict.objects.all()        # 課程機(jī)構(gòu)進(jìn)行分頁(yè)
        try:
            page = request.GET.get('page', 1)        except PageNotAnInteger:
            page = 1
        p = Paginator(all_orgs, 5, request=request)
        orgs = p.page(page)        return render(request, 'org-list.html', {            "all_orgs": orgs,            "all_citys": all_citys,            'org_nums': org_nums,
        })
通過(guò)傳過(guò)來(lái)的orgs,自動(dòng)生成頁(yè)腳
{#  分頁(yè)#}
                    <div class="pageturn">
                        <ul class="pagelist">
                            {#  如果有,獲取上一頁(yè) #}
                            {% if all_orgs.has_previous %}
                                <li class="long"><a href="?{{ all_orgs.previous_page_number.querystring }}"
                                                    class="page">上一頁(yè)</a></li>
                            {% endif %}
                            
                            {# 判斷剩余頁(yè)面的顯示方法: #}
                            {% for page in all_orgs.pages %}
                            
                                {% if page %}
                                
                                    {# 判斷page和 all_orgs.number是否相等 : }
                                    {% ifequal page all_orgs.number %}
                                        <li class="active"><a href="?{{ page.querystring }}">{{ page }}</a></li>
                                    {% else %}
                                        <li><a href="?{{ page.querystring }}" class="page">{{ page }}</a></li>
                                    {% endifequal %}
                                {% else %}
                                    <li class="none"><a href="">...</a></li>
                                {% endif %}
                            {% endfor %}
                            
                            {#  如果有,獲取下一頁(yè) #}
                            {% if all_orgs.has_next %}
                                <li class="long"><a href="?{{ all_orgs.next_page_number.querystring }}">下一頁(yè)</a></li>
                            {% endif %}
                            
                        </ul>
                    </div>

總結(jié)

切記:做分頁(yè)獲取數(shù)據(jù)要調(diào)用 .object_list
{% for course_org in all_orgs.object_list %}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI