1. 程式人生 > >django 分頁 程式碼分享

django 分頁 程式碼分享

#自定義分頁


from app01 import models
from django.utils.safestring import mark_safe
def index(request,page):
try:
page=int(page)
except Exception,e:
page = 1
start = (page-1)*5
end = page*5
per_item = 5
count = models.Host.objects.all().count()
result = models.Host.objects.all()[start:end]
temp = divmod(count,per_item)
if temp[1] == 0:
all_page_count = temp[0]
else:
all_page_count = temp[0] + 1
page_html = []
first_html = "<a href='/index/%d'>首頁</a>" %(1,)
page_html.append(first_html)
if page <= 1:
prev_html = '<a href='#'>上一頁</a>'
else:
prev_html = '<a href='/index/%d'>上一頁</a>'
page_html.append(prev_html)
begin = page -6
end = page + 5
if all_page_count < 11:
begin = 0
end = all_page_count
else:
if page < 6:
begin = 0
end = 11
else:
if page + 6 > all_page_count:
begin = page - 5
end = all_page_count
else:
begin = page -5
end = page + 6


//for i in range(all_page_count):
for i in range(begin,end):
if page == i+1:
a_html = "<a style='color:red' href='/index/%d'>%d</a>" %(i+1,i+1)
else:
a_html = "<a href='/index/%d'>%d</a>" %(i+1,i+1)
a_html = "<a href='/index/%d'>%d</a>" %(i+1,i+1)
page_html.append(a_html)
if page < 4:
next_html = "<a href='/index/%d'>下一頁</a>" %(page+1,)
else:
next_html ="<a href='#'>下一頁</a>"
page_html.append(next_html)
end_html = "<a href='/index/%d'>尾頁</a>" %(all_page_count,)
page_html.append(end_html)
page = mark_safe(''.join(page_html))
ret = {'data':result,'count':count,'page':page} 
return render_to_response('index.html',ret)






<html>
<head>
<meta http-equiv='Content-Type' content='text/html';>
<title></title>
</head>
<body>
<h1>主機資訊</h1>
<table border="1">
{% for item in data %}
<tr>
<td>{{ item.hostname }}</td>
<td>{{ item.ip }}</td>
</tr>
{% endfor %}
</table>
<div>總條數:{{count}}</div>
<div>
<a href='/index/1'>首頁</a>
<a href='/index/1'>1</a>
<a href='/index/2'>2</a>
<a href='/index/3'>3</a>
<a href='/index/4'>4</a>
</div>
</body>
</html>




#urls.py
from django.contrib import admin
from django.urls import path ,include


urlpatterns = [
    path('index/(\d*)',include('app1.urls'))
]




#寫一個處理異常的方法
def try_int(arg,default):
try:
arg = int(arg)
except Exception,e:
arg = default

return arg 

=========================================================

定義函式和類,用來簡化上述程式碼:

class PageInfo:
def __init__(self,current_page,all_count,per_item):
self.CurrentPage = current_page
self.AllCount = all_count
self.PerItem = per_item
@property
def start(self):
return (self.CurrentPage - 1)*self.PerItem
@property
def end(self):
return (self.CurrentPage)*self.PerItem
@property
def all_page_count(self):


temp = divmod(self.AllCount,self.PerItem)
if temp[1] == 0:
all_page_count = temp[0]
else:
all_page_count = temp[0] + 1
return all_page_count


def pager(page,all_page_count):
page_html=[]
first_html = "<a href='/index/%d'>首頁</a>" %(1,)
page_html.append(first_html)
if page <= 1:
prev_html = '<a href='#'>上一頁</a>'
else:
prev_html = '<a href='/index/%d'>上一頁</a>'
page_html.append(prev_html)
for i in range(all_page_count):
if page == i+1:
a_html = "<a style='color:red' href='/index/%d'>%d</a>" %(i+1,i+1)
else:
a_html = "<a href='/index/%d'>%d</a>" %(i+1,i+1)
a_html = "<a href='/index/%d'>%d</a>" %(i+1,i+1)
page_html.append(a_html)
if page < 4:
next_html = "<a href='/index/%d'>下一頁</a>" %(page+1,)
else:
next_html ="<a href='#'>下一頁</a>"
page_html.append(next_html)
end_html = "<a href='/index/%d'>尾頁</a>" %(all_page_count,)
page_html.append(end_html)
page_string = mark_safe(''.join(page_html))
return page_string

相關推薦

django 程式碼分享

#自定義分頁from app01 import modelsfrom django.utils.safestring import mark_safedef index(request,page):try:page=int(page)except Exception,e:pa

Python學習day13 Django /Session/Cookie

min art .post sso quest mit out -- asset Created on 2017年5月3日 第1課:上節作業簡介 鼠標放上表單後進入編輯模式-----需學習----CMDB裏會學到模態對話框裏--彈出對話框---對後臺進行操作---方式:1

Django

end ref -o clas 取數 lte obj idp 取數據 html頁面: <div id="pagination"> <ul id="pagination-flickr"> {% if article_list.has_

django及搜索後如何翻

如何 我們 page 翻頁 gin 需要 log not url路徑 django自帶了Pagnator 導入 from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage 分頁 de

Python之路65-Django、自定義

python目錄一、XSS二、分頁1三、分頁2四、分頁3一、XSS有下面一段代碼,想將這段代碼中的字符串渲染到HTML頁面中,默認Django是不能這樣去操作的views.pydef user_list(request): page_str = """ <a href="/use

Django和查詢參數的問題

內容 pytho 傳參 earch ear 現實 dpm .net repl 查詢是通過get的方式,之前沒有分頁之前,url是這樣的: http://hostname/search?query=port%3A8080 那麽我的想法是如果分頁了。 1,不帶page參數了。n

django django-pure-pagination

pre 自帶 provide ... ani log 一個 esp https 雖然django自帶了一個paginator,但不是很方便,我們使用django-pure-pagination github地址https://github.com/jamespacileo/

django功能

int 記錄 最大 test obj 請求 import earch eno 采用django自帶的Paginator功能 from django.core.paginator import Paginator food = foodInfo.objects.filter(

Django-

print current div set 批量 後臺 oct ews append 一、分頁試圖函數 >>> from django.core.paginator import Paginator >>> objects = [

純CSS制作自適應條-分享------彭記(019)

ans ext point ase body 20px inter port 使用 純css制作自適應分頁條 效果圖: html: <!DOCTYPE html> <html> <head> <meta charset="U

Django--器(paginator)、Django的用戶認證、Django的FORM表單

modelform 從數據 submit fields 個人 select method rtc 無法使用 分頁器(paginator) >>> from django.core.paginator import Paginator >>&g

django器paginator使用簡單說明

django 分頁Django之分頁功能Django提供了一個新的類來幫助你管理分頁數據,這個類存放在django/core/paginator.py.它可以接收列表、元組或其它可叠代的對象。基本語法class Paginator(object): def __init__(self, objec

Django

digi edi disable res short 啟動 實現 需要 creat Django分頁器 當首頁展示內容過多時,需要使用分頁來講數據傳遞給用戶,這時就可以用到Django提供的分頁器; 批量向數據庫中插入數據 1.創建表:models.py from djan

7 Django器文章

src http 技術分享 alt .com bsp 9.png image png 1 2 3 4 5 7 Django分頁器文章分頁

[py][mx]django第三方模塊django-pure-pagination

ati url course count() nat src eno dict install 分頁模塊django-pure-pagination - 一款基於django pagination封裝的更好用的分頁模塊 https://github.com/jamespac

django 與美化

django paginator django提供了分頁功能接口,比自己用切片來實現方便很多。利用django的Paginator分頁類,加上booststrap美化前端,實現分頁功能。目標效果如下: a) 只顯示n個標簽頁,如5。b) 選中變色。c) 上一頁下一頁功能。d) 若當前選中第一頁,則不

Cookie、Session和Django

{} 最大 開開 patch 裝飾器 由於 登錄驗證 mat cbv cookie Cookie的由來 大家都知道HTTP協議是無狀態的。 無狀態的意思是每次請求都是獨立的,它的執行情況和結果與前面的請求和之後的請求都無直接關系,它不會受前面的請求響應情況直接影響,也

第八篇Django

name 數據庫中獲取數據 alex 頁面 base 指南 init RM req Django分頁 1.復雜版 data = [] for i in range(1, 302): tmp = {"id": i, "name": "alex-{}".forma

django的兩種方式

data number 數據返回 django 當前 star instance previous ref 第一種自定義分頁: def pageDemo(request): ‘‘‘ 自定義分頁] :param request: :return:

django

data 結束 utf-8 切片 信息展示 def itl true django 分頁 view.py # 分頁 def paging(request): # 叢數據庫表取出來的數據總數 count_data = len(message.objects.