Python Django 簡單分頁的實(shí)現(xiàn)代碼解析
這篇文章主要介紹了Python Django 簡單分頁的實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
models.py:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=32)
def __str__(self):
return self.title
class Meta:
db_table = "books"
批量創(chuàng)建 106 條數(shù)據(jù)
import os
if __name__ == '__main__':
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite3.settings")
import django
django.setup()
from app01 import models
# 106 個(gè)書籍對(duì)象
objs = [models.Book(title="《Python 的故事第{}版》".format(i)) for i in range(116)]
# 在數(shù)據(jù)庫中批量創(chuàng)建, 10 次一提交
models.Book.objects.bulk_create(objs, 10)
views.py:
from django.shortcuts import render
from app01 import models
def book_list(request):
# 從 URL 中取參數(shù)
page_num = request.GET.get("page")
print(page_num, type(page_num))
page_num = int(page_num)
# 定義兩個(gè)變量保存數(shù)據(jù)從哪兒取到哪兒
data_start = (page_num-1)*10
data_end = page_num*10
# 書籍總數(shù)
total_count = models.Book.objects.all().count()
# 每一頁顯示多少條數(shù)據(jù)
per_page = 10
# 總共需要多少頁碼來顯示
total_page, m = divmod(total_count, per_page)
if m:
total_page += 1
all_book = models.Book.objects.all()[data_start:data_end]
# 拼接 html 的分頁代碼
html_list = []
for i in range(1, total_page+1):
tmp = '<li><a href="/book_list/?page={0}" rel="external nofollow" >{0}</a></li>'.format(i)
html_list.append(tmp)
page_html = "".join(html_list)
return render(request, "book_list.html", {"books": all_book, "page_html": page_html})
book_list.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>書籍列表</title>
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css" rel="external nofollow" >
</head>
<body>
<div class="container">
<table class="table table-bordered">
<thead>
<tr>
<th>序號(hào)</th>
<th>id</th>
<th>書名</th>
</tr>
</thead>
<tbody>
{% for book in books %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ book.id }}</td>
<td>{{ book.title }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<nav aria-label="Page navigation">
<ul class="pagination">
{{ page_html|safe }}
</ul>
</nav>
</div>
</body>
</html>
運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)ip地址查詢經(jīng)緯度定位詳解
這篇文章主要介紹了python實(shí)現(xiàn)ip地址查詢經(jīng)緯度定位詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
requests和lxml實(shí)現(xiàn)爬蟲的方法
下面小編就為大家?guī)硪黄猺equests和lxml實(shí)現(xiàn)爬蟲的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Python Selenium Cookie 繞過驗(yàn)證碼實(shí)現(xiàn)登錄示例代碼
這篇文章主要介紹了Python Selenium Cookie 繞過驗(yàn)證碼實(shí)現(xiàn)登錄示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Python實(shí)現(xiàn)快速傅里葉變換的方法(FFT)
這篇文章主要介紹了Python實(shí)現(xiàn)快速傅里葉變換的方法(FFT),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
Ubuntu配置Python環(huán)境的超詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于Ubuntu配置Python環(huán)境的超詳細(xì)教程,文中通過代碼示例將配置的過程介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-08-08
Python3實(shí)現(xiàn)發(fā)送QQ郵件功能(html)
這篇文章主要為大家詳細(xì)介紹了Python3實(shí)現(xiàn)發(fā)送QQ郵件功能,html格式的qq郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12

