django 分页
2016-04-06 15:10
405 查看
django 分页,urls
views
html,index.html
modles.py
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ #url(r'^admin/', admin.site.urls), url(r'^index/(\d*)/', views.index), ]
views
#!/usr/bin/env python #_*_coding:utf-8_*_ from django.shortcuts import render,render_to_response,redirect,HttpResponse from app01 import models from django.utils.safestring import mark_safe import common # Create your views here. def index(request,page): pageint = 5 page = common.try_int(page) startpage = (page-1) * pageint #根据url传过来的参数判断开始条数 endpage = page * pageint #根据url传过来的参数判断结束条数 hostall = models.Host.objects.all()[startpage:endpage] #一页显示的条数 hostcount= models.Host.objects.all().count() #获取数据库的总共条数 #判断总页数,如果能整除5,页数不加一, 不过不能整除5,需要加一 tempcountpage = divmod(hostcount,pageint) if tempcountpage[1] == 0: all_page_count = tempcountpage[0] else: all_page_count = tempcountpage[0] +1 #定义一个列表,然后循环总页数,追加到列表中, page_html = [] #首页 first_page = '<a href="/index/%d">首页</a>'%(1) page_html.append(first_page) #上一页 if page <= 1: up_page = '<a href="/index/%d">上一页</a>'%(1) else: up_page = '<a href="/index/%d">上一页</a>'%(page - 1) page_html.append(up_page) #显示1。。。最后的页数 for i in range(all_page_count): if page == i +1: temp_html = '<a class="selected" href="/index/%d">[%d]</a>'%(i+1,i+1) else: temp_html = '<a href="/index/%d">[%d]</a>'%(i+1,i+1) page_html.append(temp_html) #下一页 if page >= all_page_count: next_page = '<a href="/index/%d">[下一页]</a>' % (all_page_count) else: next_page = '<a href="/index/%d">[下一页]</a>'%(page + 1) page_html.append(next_page) # 尾页 end_page = '<a href="/index/%d">[尾页]</a>' %(all_page_count) page_html.append(end_page) # 使用mark_safe 可让html显示返回的a标签。join 把列表取出用空格连接 page_html = mark_safe(' '.join(page_html)) ret = {'data':hostall,'count':hostcount,'page_ret':page_html} return render_to_response('index.html',ret)
html,index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> body { font-size: .85em; font-family:"Trebuchet MS",Verdana,Helvetica,"Sakkal Majalla"; color: #232323; background-color: #ffffff; } #Pageing A{ background-color:aliceblue; border: 1px solid white; text-decoration: none; color:black; padding: .1em .6em .2em .6em;} #Pageing A.selected{ background-color: #AA7700; } </style> </head> <body> <table border="1px"> <tr> <td>HostName</td> <td>IP</td> </tr> {% for item in data %} <tr> <td>{{ item.HostName }}</td> <td>{{ item.ip }}</td> </tr> {% endfor %} </table> <div style="color: black;font-size: medium"> 共计{{ count }}条数据</div> <div id="Pageing"> {{ page_ret }}</div> </body> </html>
modles.py
from __future__ import unicode_literals from django.db import models # Create your models here. class Host(models.Model): HostName = models.CharField(max_length=256) ip = models.GenericIPAddressField()
相关文章推荐
- 20 Tips To Use Google Search Efficiently
- codeforces_598D. Igor In the Museum(dfs)
- django之 文件上传功能(缺陷:无法改存放目录)
- django之创建第12个项目-加载图片
- django之创建第11个项目-页面整合
- category
- reload、replace、href、assign、window.history.go(0)的区别
- Go语言中的<断言>
- Action & Golf 四个魔法球实战训练系列_huatuo_新浪博客
- 工作笔记之安卓开机动画与logo修改
- CodeForces 658C Bear and Forgotten Tree 3(构造)
- 我与小娜(48):AlphaGo的增援学习是什么?
- django 学习 --- 环境搭建
- BZOJ 4281 [ONTAK2015]Związek Harcerstwa Bajtockiego 树上倍增
- django之创建第10-1个项目-图片上传并记录上传时间
- 【KM】POJ2195/HDU1533-Going home
- 深度解读 AlphaGo 算法原理
- django的下载安装以及第一个项目的创建
- 关于google文章分享
- Going Together