django封裝搜尋後的分頁切換連線

2022-07-21 23:03:31 字數 3909 閱讀 3867

封裝的分頁功能

page.py

#

!/usr/bin/env python

#-*- coding: utf-8 -*-

#time : 2020/6/7 18:38

#author : eunice

import

refrom django.utils.safestring import

mark_safe

class

mypagenation(object):

def__init__(self, page_num, total_count, base_url, per_page_num=10, page_num_show=5, get_data=none):

""":param page_num: 當前頁碼數

:param total_count: 總的資料量

:param per_page_num: 每頁顯示的行數

:param get_data: 搜尋時的引數

"""self.get_data = get_data #

搜尋條件

self.per_page_num = per_page_num #

每頁顯示10行資料

self.page_num_show = page_num_show #

頁面生成分頁頁碼的數量

self.base_url =base_url

quotient, remainder = divmod(total_count, self.per_page_num) #

quotient商, remainder餘數

#總頁碼數

ifremainder:

page_num_count = quotient + 1 #

有餘數 頁數+1

else

: page_num_count =quotient

self.page_num_count =page_num_count

#相容當前頁碼數

try:

page_num =int(page_num)

except

exception:

page_num = 1

if page_num <0:

page_num = 1

elif page_num > page_num_count: #

相容輸入大於總頁碼的值

page_num = page_num_count #

返回最後一頁

half_num = self.page_num_show // 2

"""3 4 5 6 7 5 6 7 8 9

"""if page_num - half_num <= 0: #

頁碼為負數時

start_num = 1end_num = self.page_num_show #

顯示分頁的

elif page_num + half_num > page_num_count: #

頁碼大於總的頁碼數

start_num = page_num_count - self.page_num_show + 1end_num =page_num_count

else

: start_num = page_num - half_num #

開始頁碼

end_num = page_num + half_num #

結束頁碼

if page_num_count < page_num_show: #

總的頁碼數小於展示的頁碼數

start_num = 1end_num =page_num_count

self.page_num =page_num

self.start_num =start_num

self.end_num =end_num

@property

defcal_start_data(self):

return (self.page_num - 1) *self.per_page_num

@property

defcal_end_data(self):

return self.page_num *self.per_page_num

@property

defhtml_page(self):

page_num_range = range(self.start_num, self.end_num + 1)

page_html = ''

page_pre_html = '

'last_page_html = '

最後一頁

'.format(

self.page_num_count, self.base_url, re.sub(

'page=\d+

', '', self.get_data) if

'page=

'in self.get_data else self.get_data + '

&') #

尾頁標籤

page_html +=last_page_html

page_html +=page_next_html

return mark_safe(page_html)

views.py

def

customer(request):

get_data = request.get.urlencode() #

search_field=name__contains&kw=%e4%b8%9c

search_field = request.get.get('

search_field

') #

選擇搜尋字段

kw = request.get.get('

kw') #

搜尋關鍵字

ifkw:

#customer_list = models.customer.objects.filter(**)

#另一種實現方式,支援或查詢

q_obj = q() #

q條件查詢的連線符預設是&連線

可以拿到一組關鍵字 q(qq=kw)##

q_obj.connector = 'or' # 修改連線符為or q(qq=kw)|q(name=kw)

customer_list =models.customer.objects.filter(q_obj)

else

: customer_list =models.customer.objects.all()

page_num = request.get.get('

page

') #

獲取當前頁碼數

customer_count =customer_list.count()

per_page_num =settings.per_page_num

page_num_show =settings.page_num_show

base_url = request.path #

訪問的路徑

page_obj =mypage(page_num, customer_count, base_url, per_page_num, page_num_show, get_data)

customer_obj =customer_list.reverse()[page_obj.cal_start_data: page_obj.cal_end_data]

#return render(request, 'saleshtml/customers.html', )

return render(request, '

saleshtml/customers.html

', )

分頁,搜尋後保留搜尋條件分頁

先寫乙個簡單的分頁,供初學者參考 header content type text html charset utf8 建立乙個資料來源,若資料來自於mysql 此處請改為mysql獲取資料 注意limit使用 limit offset,length offset 為偏移量,從第幾個開始 下標從0開...

Django 簡單實現分頁與搜尋功能

假設現有需求如下 需要乙個頁面分頁展示資訊,在該頁面新增搜尋框以提供檢索功能。那麼,我們知道,展示資訊和檢索功能是在同乙個頁面,也就是共用乙個路由。如下 第一步,寫路由 為了清晰,這裡只給出主頁和展示頁面的路由。urls.py from django.urls import path from im...

django的分頁機制

翻譯自官方文件 django 1.0 中分頁機制和先前已經大不相同。它提供了一些類協助你把資料分頁。對應的檔案為 django core paginator.py 類paginator,帶兩個構造引數,乙個就是資料的集合,另乙個表示每頁放幾個資料。from django.core.paginator...