中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Django 學習之 分頁器的使用

發布時間:2020-07-02 20:15:19 來源:網絡 閱讀:332 作者:水滴石川1 欄目:編程語言


# 創建項目 page_project


配置settings?



啟動??

# 導入分頁器相關模塊


from?django.core.paginator?import?Paginator,?EmptyPage,?PageNotAnInteger


查看urls?

from?app01?import?views

urlpatterns?=?[
????url(r'^admin/',?admin.site.urls),
????url(r'^index/',?views.index),
]


查看views ,測試一條數據? ,準備數據100條

def?index(request):
????#?插入一條數據
????#?book_1?=?Book(title='python',?price="60")
????#?book_1.save()
????Booklist?=?[]
????for?i?in?range(100):
????????Booklist.append(Book(title="book"?+?str(i),?price=30?+?i?*?2))
????Book.objects.bulk_create(Booklist)
????
????"""
????:param?request:
????:return:
????"""
????return?HttpResponse("index")


? ? 查看? 一條數據 sqllite?

Django 學習之 分頁器的使用


? ?查看插入的100條數據?


Django 學習之 分頁器的使用


Django 學習之 分頁器的使用


功能1、展示數據里面的所有數據

def?index(request):
????#?分頁器
????book_list?=?Book.objects.all()
????print(book_list,?type(book_list))
????return?render(request,?"index.html",?locals())

html 頁面


Django 學習之 分頁器的使用


前端頁面顯示如下

Django 學習之 分頁器的使用


功能2、分頁器常用的功能


views?

book_list?=?Book.objects.all()
paginator?=?Paginator(book_list,?3)??#?每頁顯示三條數據
print(paginator,?type(paginator))

print("count:",?paginator.count)??#?數據總數
print("num_pages",?paginator.num_pages)??#?總頁數
print("page_range",?paginator.page_range)??#?頁碼的列表


訪問頁面刷新獲取數據

Django 學習之 分頁器的使用


# 顯示首頁數據


views?

first_page?=?(request.GET.get())
first_page_data?=?paginator.page(first_page)

index .html

顯示首頁?{%?%}
????{{?.}}?---{{?.}}
????????{%?%}


前端訪問顯示


Django 學習之 分頁器的使用

功能三、根據請求頁面數顯示不同頁面的數據,不存在的頁面數據就顯示首頁

try:
????current_page_number?=?int(request.GET.get("page",?1))
????if?current_page_number?<?34:
????????current_page?=?paginator.page(current_page_number)
????elif?current_page_number?<?0:
????????current_page?=?paginator.page(current_page_number)
????else:
????????current_page?=?paginator.page(1)
except?EmptyPage?as?e:
????current_page?=?paginator.page(1)


index.html??

當前頁面為??{%?%}
????{{?.}}?---{{?.}}
????????{%?%}


訪問展示


超出34 頁面默認顯示首頁

Django 學習之 分頁器的使用


功能四、引入bootstrap 和 分頁 格式 豐富樣式


views?

def?index(request):
????#?分頁器
????book_list?=?Book.objects.all()
????paginator?=?Paginator(book_list,?10)??#?每頁顯示三條數據
????print(paginator,?type(paginator))
????page_range?=?paginator.page_range

????print("count:",?paginator.count)??#?數據總數
????print("num_pages",?paginator.num_pages)??#?總頁數
????print("page_range",?paginator.page_range)??#?頁碼的列表

????#?根據請求頁面數顯示不同頁面的數據,不存在的頁面數據就顯示首頁

????try:
????????current_page_num=?int(request.GET.get("page",?1))
????????current_page?=?paginator.page(current_page_num)
????????print(current_page,?type(current_page))
????????print("object_list",?current_page.object_list)
????except?EmptyPage?as?e:
????????current_page?=?paginator.page(1)
????return?render(request,?"index.html",?locals())


index? 頁面

<!DOCTYPE?html>
<html?lang="en">
<head>
????<meta?charset="UTF-8">
????<title>?index</title>

????<link?rel="stylesheet"?href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"?integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh5u"?crossorigin="anonymous">
????
</head>
<body>

<br>
<p>?當前頁面為?</p>
<ul>
{%?for?book?in?current_page?%}
????<li>{{?book.title?}}:{{?book.price?}}</li>
?{%?endfor?%}
</ul>

<p>?顯示頁碼??</p>

<nav?aria-label="Page?navigation">
??<ul?class="pagination">
????<li>
??????<a?href="#"?aria-label="Previous">
????????<span?aria-hidden="true">?上一頁</span>
??????</a>
????</li>
??
??<p>?循環遍歷?頁碼數?</p>
?{%?for?item?in?paginator.page_range?%}
?
??<p>?當顯示當前頁面的時候加藍?</p>
??
??????????{%?if?current_page_num?==?item?%}
???????????<li?class="active"><a?href="?page={{?item?}}">?{{?item?}}?</a>?</li>
?{%?else?%}
????????<li><a?href="?page={{?item?}}">?{{?item?}}?</a>?</li>
?{%?endif?%}
??????
??????{%?endfor?%}
??????
????<li>
??????<a?href="#"?aria-label="Next">
????????<span?aria-hidden="true">?下一頁??</span>
??????</a>
????</li>
??</ul>
</nav>


</body>
</html>


頁面效果


Django 學習之 分頁器的使用


顯示上一頁,下一頁?

# 根據循環當前的頁面 進行判斷? ?current_page


上一頁使用???current_page|add:-1? ?或者??current_page.previous_page_number?

下一頁 使用?current_page|add:+1 或者??current_page.next_page_number


<li>
????{%?if??current_page.has_previous?%}
????????<a?href="?page?={{?current_page|add:-1?}}"?aria-label="Previous">
????????????{#??????<a?href="?page?={{?current_page.previous_page_number?}}"?aria-label="Previous">#}??
????????????<span?aria-hidden="true">?上一頁</span>
????????</a>
????{%?else?%}
????????<li?class="disabled"><a?href=""?aria-label="Previous"><span?aria-hidden="true">上一頁</span></a></li>

????{%?endif?%}
</li>


<li>
????{%?if?current_page.has_next?%}
????????<a?href="?page=?{{?current_page|add:+1}}"?aria-label="Next">

????????????{#??????<a?href="?page=?{{?current_page.next_page_number?}}"?aria-label="Next">#}
????????????<span?aria-hidden="true">?下一頁??</span>
????????</a>
????{%?else?%}
????????<li?class="disabled"><a?href=""?aria-label="Next"><span?aria-hidden="true">下一頁</span></a>
????{%?endif?%}
????</li>


查看頁面?

Django 學習之 分頁器的使用



顯示默認的五頁


views? 代碼


if?paginator.num_pages?>?11:
????if?current_page_num?-?5?<?1:
????????page_range?=?range(1,?12)
????elif?current_page_num?+?5?>?paginator.num_pages:
????????page_range?=?range(paginator.num_pages?-?10,?paginator.num_pages?+?1)
????else:
????????page_range?=?range(current_page_num?-?5,?current_page_num?+?6)
else:
????page_range?=?paginator.page_range


html?

<!DOCTYPE?html>
<html?lang="en">
<head>
????<meta?charset="UTF-8">
????<title>?index</title>
????<link?rel="stylesheet"?
?integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh5u"?crossorigin="anonymous">
</head>
<body>

<br>
<p>?當前頁面為?</p>
<ul>
?{%?for?book?in?current_page?%}
????????<li>{{?book.title?}}:{{?book.price?}}</li>
?{%?endfor?%}
</ul>

<p>?顯示頁碼?</p>

<nav?aria-label="Page?navigation">
????<ul?class="pagination">
????????<li>
?{%?if??current_page.has_previous?%}
????????????????<a?href="?page?={{?current_page|add:-1?}}"?aria-label="Previous">
?{#??????<a?href="?page?={{?current_page.previous_page_number?}}"?aria-label="Previous">#}
?<span?aria-hidden="true">?上一頁</span>
????????????????</a>
?{%?else?%}
????????????????<li?class="disabled"><a?href=""?aria-label="Previous"><span?aria-hidden="true">上一頁</span></a></li>

?{%?endif?%}
????????</li>

?{%?for?item?in?page_range?%}

????????????{%?if?current_page_num?==?item?%}
????????????????<li?class="active"><a?href="?page={{?item?}}">{{?item?}}</a></li>
?{%?else?%}
????????????????<li><a?href="?page={{?item?}}">{{?item?}}</a></li>
?{%?endif?%}

????????{%?endfor?%}

????????<li>
?{%?if?current_page.has_next?%}
????????????????<a?href="?page=?{{?current_page|add:+1}}"?aria-label="Next">

?{#??????<a?href="?page=?{{?current_page.next_page_number?}}"?aria-label="Next">#}
?<span?aria-hidden="true">?下一頁??</span>
????????????????</a>
?{%?else?%}
????????????????<li?class="disabled"><a?href=""?aria-label="Next"><span?aria-hidden="true">下一頁</span></a>
?{%?endif?%}
????????????</li>
????</ul>

</nav>


</body>
</html>


頁面顯示?

Django 學習之 分頁器的使用



Django 學習之 分頁器的使用



向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

庄浪县| 扎鲁特旗| 博爱县| 晋中市| 安龙县| 汉沽区| 安康市| 宁南县| 烟台市| 廊坊市| 刚察县| 宿迁市| 鸡西市| 遂溪县| 乐亭县| 惠州市| 千阳县| 连云港市| 宜君县| 桦南县| 南江县| 藁城市| 余江县| 施秉县| 福海县| 双辽市| 芜湖市| 砀山县| 平果县| 达孜县| 祁阳县| 潮州市| 安岳县| 望江县| 阿城市| 徐州市| 肥乡县| 洛隆县| 习水县| 南平市| 纳雍县|