欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django前端BootCSS實(shí)現(xiàn)分頁的方法

 更新時(shí)間:2021年11月02日 15:19:05   作者:lyshark  
本文主要介紹了Django前端BootCSS實(shí)現(xiàn)分頁的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

通過使用bootstrap框架,并配合Django自帶的Paginator分頁組件即可實(shí)現(xiàn)簡單的分頁效果。

1.創(chuàng)建MyWeb項(xiàng)目

python manage.py startapp MyWeb

2.修改settings.py配置文件,導(dǎo)入我們的app的名字,去掉csrf這個(gè)選項(xiàng)

# 屏蔽一項(xiàng)
MIDDLEWARE = [
    #'django.middleware.csrf.CsrfViewMiddleware'
]

# 新增一項(xiàng)
TEMPLATES = [
 'MyWeb.apps.MywebConfig'
]

3.來urls.py里面寫一條路由,名字就叫index/映射到views.index函數(shù)下處理此請求

from MyWeb import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index)
]

4.最后在myweb里面的views.py設(shè)置一個(gè)視圖函數(shù),最后運(yùn)行

from django.shortcuts import render
from django.shortcuts import HttpResponse
from MyWeb import models

def index(requests):
    return HttpResponse("abcd")

5.配置數(shù)據(jù)庫文件models.py并設(shè)置以下內(nèi)容

from django.db import models

# 創(chuàng)建用戶表
class User(models.Model):
    id = models.AutoField(primary_key=True)
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=32)

6.更新數(shù)據(jù)庫與數(shù)據(jù)表

python manage.py makemigrations   # 將你的數(shù)據(jù)庫變動(dòng)記錄下來(并不會(huì)幫你創(chuàng)建表)
python manage.py migrate          # 將你的數(shù)據(jù)庫變動(dòng)正在同步到數(shù)據(jù)庫中

7.增加一個(gè)新的view并使用rand()函數(shù).

首先在urls.py中增加路由

from django.contrib import admin
from django.urls import path
from MyWeb import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/',views.index),
    path('rand/',views.rand)
]

其次在view.py視圖中增加生成函數(shù).

from django.shortcuts import render
from django.shortcuts import HttpResponse
from MyWeb import models
import random

# 首頁
def index(requests):
    return HttpResponse("abcd")

# 生成測試數(shù)據(jù)
def rand(request):
    for i in range(1,1000):
        chars = []
        pasd = []
        for x in range(1,8):
            chars.append(random.choice('abcdefghijklmnopqrstuvwxyz'))
            pasd.append(random.choice('0987654321'))
        user = "".join(chars)
        pwd = "".join(pasd)
        models.User.objects.create(username=user, password=pwd)
    return HttpResponse("ok")

啟動(dòng)django并訪問http://127.0.0.1:8000/rand/等待數(shù)據(jù)生成結(jié)束.

8.在templates模板中,新增一個(gè)page.html頁面。

<!--name: page.html-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet"  rel="external nofollow" >
</head>
<body>
<table class="table table-sm table-hover">
    <thead>
        <tr class="table-success">
            <th> 序號</th> <th> 用戶名</th> <th> 用戶密碼</th>
        </tr>
    </thead>
    <tbody>
        {% for article in user_list %}
            <tr class="table-primary">
                <td>{{ article.id }}</td>
                <td>{{ article.username }}</td>
                <td>{{ article.password }}</td>
            </tr>
        {% endfor %}
    </tbody>
</table>
<nav class="d-flex justify-content-center" aria-label="Page navigation example">
    <ul class="pagination">
        <li class="page-item"><a class="page-link" href="./page?id=1" rel="external nofollow" >首頁</a></li>
        {% if user_list.has_previous %}
            <li class="page-item"><a class="page-link" href="./page?id={{ user_list.previous_page_number }}" rel="external nofollow" >上一頁</a></li>
        {% else %}
            <li class="page-item disabled"><a class="page-link" href="#" rel="external nofollow"  rel="external nofollow" >上一頁</a></li>
        {% endif %}

        {% for item in page_range %}
            {% if item == currentPage %}
                <li class="page-item active"><a class="page-link" href="./page?id={{ item }}" rel="external nofollow"  rel="external nofollow" >{{ item }}</a></li>
            {% else %}
                <li class="page-item"><a class="page-link" href="./page?id={{ item }}" rel="external nofollow"  rel="external nofollow" >{{ item }}</a></li>
            {% endif %}
        {% endfor %}

        {% if user_list.has_next %}
            <li class="page-item"><a class="page-link" href="./page?id={{ user_list.next_page_number }}" rel="external nofollow" >下一頁</a></li>
        {% else %}
            <li class="page-item disabled"><a class="page-link" href="#" rel="external nofollow"  rel="external nofollow" >下一頁</a></li>
        {% endif %}
        <li class="page-item"><a class="page-link" href="./page?id={{ paginator.num_pages }}" rel="external nofollow" >尾頁</a></li>
    </ul>
</nav>

<div style="text-align: center;" class="alert alert-dark">
   統(tǒng)計(jì): {{ currentPage }}/{{ paginator.num_pages }} 共查詢到:{{ paginator.count }} 條數(shù)據(jù) 頁碼列表:{{ paginator.page_range }}
</div>
</body>
</html>

9.最后在路由曾以及view中增加對應(yīng)的URL以及路由函數(shù).

首先在urls.py中增加一條新路由.

from django.contrib import admin
from django.urls import path
from MyWeb import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/',views.index),
    path('rand/',views.rand),
    path('page',views.page)
]

接著在views.py中增加一個(gè)page函數(shù).

from django.shortcuts import render
from django.shortcuts import HttpResponse
from MyWeb import models
import random

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

# 首頁
def index(requests):
    return HttpResponse("abcd")

# 生成測試數(shù)據(jù)
def rand(request):
    for i in range(1,1000):
        chars = []
        pasd = []
        for x in range(1,8):
            chars.append(random.choice('abcdefghijklmnopqrstuvwxyz'))
            pasd.append(random.choice('0987654321'))
        user = "".join(chars)
        pwd = "".join(pasd)
        models.User.objects.create(username=user, password=pwd)
    return HttpResponse("ok")

# 分頁函數(shù)
def page(request):
    user = models.User.objects.all()
    paginator = Paginator(user, 10)
    currentPage = int(request.GET.get("id",1))

    if paginator.num_pages > 15:
        if currentPage-5 < 1:
            pageRange = range(1,11)
        elif currentPage+5 > paginator.num_pages:
            pageRange = range(currentPage-5,paginator.num_pages)
        else:
            pageRange = range(currentPage-5,currentPage+5)
    else:
        pageRange = paginator.page_range

    try:
        user_list = paginator.page(currentPage)
    except PageNotAnInteger:
        user_list = paginator.page(1)
    except:
        user_list = paginator.page(paginator.num_pages)

    return render(request,"page.html",{"user_list":user_list,
                                       "paginator":paginator,
                                       "page_range":pageRange,
                                       "currentPage":currentPage})

準(zhǔn)備就緒之后,直接訪問http://127.0.0.1:8000/page即可看到分頁顯示效果.

到此這篇關(guān)于Django前端BootCSS實(shí)現(xiàn)分頁的方法的文章就介紹到這了,更多相關(guān)Django BootCSS分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python API 自動(dòng)化實(shí)戰(zhàn)詳解(純代碼)

    Python API 自動(dòng)化實(shí)戰(zhàn)詳解(純代碼)

    今天小編就為大家分享一篇Python API 自動(dòng)化實(shí)戰(zhàn)詳解(純代碼),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python dataframe如何選擇某一列非空的行

    python dataframe如何選擇某一列非空的行

    這篇文章主要介紹了python dataframe如何選擇某一列非空的行問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • 使用python將圖片按標(biāo)簽分入不同文件夾的方法

    使用python將圖片按標(biāo)簽分入不同文件夾的方法

    今天小編就為大家分享一篇使用python將圖片按標(biāo)簽分入不同文件夾的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python中字典的常見操作總結(jié)1

    python中字典的常見操作總結(jié)1

    這篇文章主要介紹了python中字典的常見操作總結(jié),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • 利用Python實(shí)現(xiàn)Shp格式向GeoJSON的轉(zhuǎn)換方法

    利用Python實(shí)現(xiàn)Shp格式向GeoJSON的轉(zhuǎn)換方法

    JSON(JavaScript Object Nonation)是利用鍵值對+嵌套來表示數(shù)據(jù)的一種格式,以其輕量、易解析的優(yōu)點(diǎn),這篇文章主要介紹了利用Python實(shí)現(xiàn)Shp格式向GeoJSON的轉(zhuǎn)換,需要的朋友可以參考下
    2019-07-07
  • Python Json讀寫操作之JsonPath用法詳解

    Python Json讀寫操作之JsonPath用法詳解

    JSONPath是一種信息抽取類庫,是從JSON文檔中抽取指定信息的工具,提供多種語言實(shí)現(xiàn)版本,包括Javascript、Python、PHP和Java,這篇文章主要介紹了Python Json讀寫操作之JsonPath用法詳解,需要的朋友可以參考下
    2023-04-04
  • PyQt5實(shí)現(xiàn)簡單的計(jì)算器

    PyQt5實(shí)現(xiàn)簡單的計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了PyQt5實(shí)現(xiàn)簡單的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • python爬蟲 批量下載zabbix文檔代碼實(shí)例

    python爬蟲 批量下載zabbix文檔代碼實(shí)例

    這篇文章主要介紹了python爬蟲 批量下載zabbix文檔代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • TensorFlow tf.nn.conv2d_transpose是怎樣實(shí)現(xiàn)反卷積的

    TensorFlow tf.nn.conv2d_transpose是怎樣實(shí)現(xiàn)反卷積的

    這篇文章主要介紹了TensorFlow tf.nn.conv2d_transpose是怎樣實(shí)現(xiàn)反卷積的,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 一文詳解Django信號機(jī)制的工作原理

    一文詳解Django信號機(jī)制的工作原理

    Django 信號(signals)是一種實(shí)現(xiàn)解耦的有力工具,它允許某些發(fā)生的事件通知其他部分的代碼,本文將深入探討 Django 信號的工作原理、如何定義和接收信號,以及如何在項(xiàng)目中有效地使用它們,需要的朋友可以參考下
    2023-11-11

最新評論