- 自定义分页一:
views:
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts import render,render_to_response from app01 import models
# Create your views here. def index(request, page):#用于获取前台get传来的数据 #操作数据库进行分页 page = int(page)#此时需要进行整数运算,所以需要强制类型转换 #per_item = 5 #第一页0-5,(page-1)*5 page*5 start = (page-1)*5 end = page*5 count = models.host.objects.all()[start:end].count()#此时就是实现了一部分一部分的获取数据 data = models.host.objects.all()[start:end] ret = {'data':data,'count':count} return render_to_response('index.html',ret)
index:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <h1>主机信息</h1> <table border="1" > {% for item in data %} <tr > <td>{{ item.hostname }}</td> <td>{{ item.ip}}</td> <br /> </tr> {% endfor %} </table> <div>总条数:{{ count }}</div> </body> </html>
urls:
from django.conf.urls import url,include from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/(d+)', views.index),#id用于获取用户输入的页数 ]
models:
# -*- coding: utf-8 -*- 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()
此时的页面:
- 自定义分页二(进化版):
views:
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts import render,render_to_response from app01 import models from django.utils.safestring import mark_safe # Create your views here. def index(request, page): #判断获取的 值是否为整数 try: page = int(page) except Exception,e: page = 1 per_item = 5 start = (page-1)*5 end = page*5 count = models.host.objects.all().count() data = models.host.objects.all()[start:end] #判断需要分几页,divmod()是一个求商和余的函数 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) for i in range(all_page_count): a_html = "<a href='/index/%d'>%d</a>"%(i+1,i+1) page_html.append(a_html) end_html = "<a href='/index/%d'>尾页</a>" % (all_page_count,) page_html.append(end_html) page = mark_safe(''.join(page_html))#想要在页面上显示关键字就必须要确认他不是类似跨站脚本的东西 ret = {'data':data,'count':count,'page':page} return render_to_response('index.html',ret)
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>主机信息</h1>
<table border="1" >
{% for item in data %}
<tr >
<td>{{ item.hostname }}</td>
<td>{{ item.ip}}</td>
<br />
</tr>
{% endfor %}
</table>
<div>总条数:{{ count }}</div>
{{ page }}
</body>
</html>
其余一样
显示页面:
- 自定义分页(第三种):
views:
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts import render,render_to_response from app01 import models from django.utils.safestring import mark_safe # Create your views here. def index(request, page): #判断获取的 值是否为整数 try: page = int(page) except Exception,e: page = 1 per_item = 5 start = (page-1)*5 end = page*5 count = models.host.objects.all().count() data = models.host.objects.all()[start:end] #判断需要分几页,divmod()是一个求商和余的函数 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: prv_html = "<a href='/index/#'>上一页</a>" else: prv_html = "<a href='/index/%d'>上一页</a>"%(page-1,) page_html.append(prv_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) page_html.append(a_html) next_html = "<a href='/index/%d'>下一页</a>" % (page+1,) 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':data,'count':count,'page':page} return render_to_response('index.html',ret)
- 自定义分页(第四种):
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts import render,render_to_response from app01 import models from django.utils.safestring import mark_safe # Create your views here. def index(request, page): #判断获取的 值是否为整数 try: page = int(page) except Exception,e: page = 1 per_item = 3 start = (page-1)*per_item end = page*per_item count = models.host.objects.all().count() data = models.host.objects.all()[start:end] #判断需要分几页,divmod()是一个求商和余的函数 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: prv_html = "<a href='/index/#'>上一页</a>" else: prv_html = "<a href='/index/%d'>上一页</a>"%(page-1,) page_html.append(prv_html) #当所分的页数特别多时,需要微量显示 if all_page_count < 11: begin = 0 end = all_page_count else: if page < 6: begin = 0 end = 12 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(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) page_html.append(a_html) next_html = "<a href='/index/%d'>下一页</a>" % (page+1,) 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':data,'count':count,'page':page} return render_to_response('index.html',ret)