数据库大概如下:
test.py中的代码如下:
#coding=utf-8 #m #m\__author__ = ‘Administrator‘ import MySQLdb #mfrom django.shortcuts import render_to_response import json def getdata(request): conn = MySQLdb.connect( host=‘192.168.8.176‘, port = 3306, user = ‘chenhuachao‘, passwd = ‘123‘, db = ‘myblog‘, ) cur=conn.cursor() # curr_page=request.GET.get("page","") # curr_page=request.GET["page"] curr_page=int(request.GET.get("page","1")) a=int(curr_page)*3 sql="select * from blogs_blog limit "+str(a) +",3" cur.execute(sql) infors=cur.fetchall() results=[] for infor in infors: results.append({‘id‘:infor[0],‘title‘:infor[1],‘author‘:infor[2],‘content‘:[3],‘post_date‘:infor[4]}) count_sql="select count(*) from blogs_blog" cur.execute(count_sql) count_sql=cur.fetchall() count=count_sql[0][0] # count1 = json.dumps(count_sql) # count2=json.loads(count) # c=int(count1) cur.close() conn.close() print count if count % 3 ==0: num_pages=count /3 #总共的页码 else: num_pages=count/3+1 #不解释 last_page=int(num_pages)-1 #最后页 int_curr_page=curr_page #获取url中的页码 if int_curr_page == 0:#判断是否有前一页 has_previous=False else: has_previous=True if int_curr_page == int(num_pages):#判断是否有下一页 has_next=False else: has_next=True previous_page_number=int_curr_page -1 #当前页的前一页 nex_page_number=int_curr_page+1 #当前页的后一页 return render_to_response(‘test.html‘,locals())
url中的定义:
urlpatterns = patterns(‘‘, # Examples: # url(r‘^$‘, ‘pagenation.views.home‘, name=‘home‘), # url(r‘^pagenation/‘, include(‘pagenation.foo.urls‘)), # Uncomment the admin/doc line below to enable admin documentation: url(r‘^admin/doc/‘, include(‘django.contrib.admindocs.urls‘)), # Uncomment the next line to enable the admin: url(r‘^admin/‘, include(admin.site.urls)), url(r‘^test/‘, getdata), )
前端代码如下:(用了bootstrap,大家都懂的)
<!DOCTYPE html> <html> <head lang="en"> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>分页显示</title> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script> </head> <body> <table align="center" class="table table-hover"> <tr><th>ID</th><th>title</th><th>author</th><th>content</th><th>日期</th></tr> {% for res in results %} <tr> <td>{{ res.id }}</td> <td>{{ res.title }}</td> <td>{{ res.author }}</td> <td>{{ res.content }}</td> <td>{{ res.post_date }}</td> </tr> {% endfor %} </table> <div align="center"> <a href="/">返回首页</a> 总记录数:{{ count }} 总页数:{{ num_pages }} 当前页:<fount color="red">{{ curr_page }}</fount> {% if has_previous %} <a href="/test/?page=0">首页</a> <a href="/test/?page={{ previous_page_number }}">上一页</a> {% else %} 首页 上一页 {% endif %} {% if has_next %} <a href="/test/?page={{ nex_page_number }}">下一页</a> <a href="/test/?page={{ last_page }}"> 尾页</a> {% else %} 下一页 尾页 {% endif %} </div> </body> </html>
具体效果:
本文出自 “在奔跑的人” 博客,请务必保留此出处http://leizhu.blog.51cto.com/3758740/1674992
原文地址:http://leizhu.blog.51cto.com/3758740/1674992