tornado&django --- 分頁(yè)
tornado
1,urls.py
import config import os from views.view import IndexHadnler,SearchHadnler,SearchresHadnler class Application(tornado.web.Application): def __init__(self): heandlers = [ (r'/',IndexHadnler), (r'/search',SearchHadnler), (r'/search_res/(?P<page>\d*)',SearchresHadnler), # (r'/pay',PayHadnler) ] super(Application,self).__init__(heandlers,**config.settings,address='0.0.0.0')
2,實(shí)現(xiàn)分頁(yè)功能及展示
class Pagination: """ 分頁(yè) """ def __init__(self, current_page='1', page_item=1): all_page, c = divmod(page_item, 5) if c > 0: all_page += 1 try: current_page = int(current_page) except: current_page = 1 if current_page < 1: current_page = 1 self.current_page = current_page # 當(dāng)前頁(yè) self.all_page = all_page # 總頁(yè)數(shù) @property def start(self): """ 顯示數(shù)據(jù)的起點(diǎn)索引 :return: """ return (self.current_page - 1) * 5 @property def end(self): """ 顯示數(shù)據(jù)的末尾索引 :return: """ return self.current_page * 5 def page_num_show(self, baseurl): """ 寫入{% raw str_page %}模板中的內(nèi)容 :param baseurl: 該段代碼不僅可以在/index/中使用,也可以在/home/等等頁(yè)碼使用, :return: 返回一段字符串形式的html代碼塊,包括首頁(yè),頁(yè)碼數(shù),上一頁(yè)等等內(nèi)容 """ # 計(jì)算9個(gè)頁(yè)碼的起始索引 list_page = [] if self.current_page <= 4: s = 0 e = min(self.all_page, 9) elif self.current_page > self.all_page - 4: s = max(0, self.all_page - 9) e = self.all_page else: s = self.current_page - 5 e = self.current_page + 4 # 首頁(yè) first_page = '<a href="%s1">首頁(yè)</a>' % (baseurl) list_page.append(first_page) # 上一頁(yè)current_page-1 if self.current_page <= 1: prev_page = '<a href="javascript:void(0);">上一頁(yè)</a>' else: prev_page = '<a href="%s%s">上一頁(yè)</a>' % (baseurl, self.current_page - 1) list_page.append(prev_page) #9個(gè)頁(yè)碼數(shù) for p in range(s, e): if p + 1 == self.current_page: temp = '<a href="%s%s" class="active">%s</a>' % (baseurl, p + 1, p + 1) list_page.append(temp) else: temp = '<a href="%s%s">%s</a>' % (baseurl, p + 1, p + 1) list_page.append(temp) # 下一頁(yè)next_page+1 if self.current_page >= self.all_page: next_page = '<a href="javascript:void(0);">下一頁(yè)</a>' else: next_page = '<a href="%s%s">下一頁(yè)</a>' % (baseurl, self.current_page + 1) list_page.append(next_page) # 尾頁(yè) last_page = '<a href="%s%s">尾頁(yè)</a>' % (baseurl, self.all_page) list_page.append(last_page) # # 頁(yè)面跳轉(zhuǎn) # jump = """<input type="text"/><a οnclick="Jump('%s',this);">go</a>""" % (baseurl,) # script = """<script> # function Jump(url,self){ # var v=self.previousElementSibling.value; # if (v.trim().length>0){ # location.href=url+v; # } # } # </script>""" # list_page.append(jump) # list_page.append(script) str_page = "".join(list_page) return str_page class SearchresHadnler(BaseHandler): async def get(self,page): page_obj = Pagination(page, len(info_res)) # 當(dāng)前頁(yè)顯示的數(shù)據(jù) current_list = info_res[page_obj.start:page_obj.end] # 當(dāng)前頁(yè)顯示的頁(yè)碼數(shù)相關(guān)html代碼 str_page = page_obj.page_num_show('/search_res/') self.render('info.html', info=current_list, current_page=page_obj.current_page, str_page=str_page)
3,info.html
<!DOCTYPE html> <!-- <html lang="en"> --> <link rel="stylesheet" type="text/css" href="../static/top/gameadmin/libs/aui/css/aui.css" /> <link rel="stylesheet" type="text/css" href="../static/top/gameadmin/libs/aui/css/bootstrap.min.css" /> <link rel="stylesheet" type="text/css" href="../static/top/gameadmin/libs/aui/css/bootstrap-theme.min.css" /> <link type="text/css" rel="stylesheet" href="../static/top/gameadmin/css/jquery.dataTables.min.css"> <link href="../static/top/gameadmin/css/H-ui.min.css" rel="stylesheet" type="text/css" /> <link href="../static/top/gameadmin/css/H-ui.admin.css" rel="stylesheet" type="text/css" /> <script src="../static/top/gameadmin/libs/jquery-1.12.1.min.js" ></script> <script src="../static/top/gameadmin/libs/bootstrap.min.js" ></script> <script src="../static/top/gameadmin/libs/axios.js" ></script> <script src="../static/top/gameadmin/libs/jquery.cookie.js" ></script> <script> var le = { language: { "sProcessing": "處理中...", "sLengthMenu": "顯示 _MENU_ 項(xiàng)結(jié)果", "sZeroRecords": "沒有匹配結(jié)果", "sInfo": "顯示第 _START_ 至 _END_ 項(xiàng)結(jié)果,共 _TOTAL_ 項(xiàng)", "sInfoEmpty": "顯示第 0 至 0 項(xiàng)結(jié)果,共 0 項(xiàng)", "sInfoFiltered": "(由 _MAX_ 項(xiàng)結(jié)果過濾)", "sInfoPostFix": "", "sSearch": "搜索:", "sUrl": "", "sEmptyTable": "表中數(shù)據(jù)為空", "sLoadingRecords": "載入中...", "sInfoThousands": ",", "oPaginate": { "sFirst": "首頁(yè)", "sPrevious": "上頁(yè)", "sNext": "下頁(yè)", "sLast": "末頁(yè)" } } } </script> <head> <!-- <meta charset="UTF-8"> --> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <!-- <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/> --> <!-- <meta name="format-detection" content="telephone=no,email=no,date=no,address=no"> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="expires" content="0" /> --> <title>碩果電影</title> </head> <body> <!-- <div class="aui-content aui-margin-b-15"> --> <section class="container-fluid page-404 minWP text-c" style="height: 20px;"> <p class="error-title"> <span class="va-m"> 電影列表</span> </p> </section> <div class="container" style="margin-top: 70px"> <div class="row"> <div class="col-md-10 col-md-offset-1"> <table id="dt" style="border:black 3px solid" class="table table-hover"> <thead><tr class="warning"><td>影片名稱</td><td>影片類別</td><td>更新時(shí)間</td><td>播放</td></tr></thead> <tbody> {% for i in info %} <tr> <td>{{i['title']}}</td> <td>{{i['typ']}}</td> <td>{{i['update_time']}}</td> <td> {%for j in i['info_url']%} <a href="{{j['url']}}">{{j['name']}}</a> {%end%} </td> </tr> {%end%} </tbody> </table> <div class="page_num"> {% raw str_page %} </div> </div> </div> </div> </div> <script type="text/javascript" src="../static/top/gameadmin/js/jquery.dataTables.min.js" ></script> <script type="text/javascript"> $(document).ready(function() { "bFilter": true, //過濾功能 $("#dt").DataTable( // "bFilter": true,//過濾功能 // 'bLengthChange': false,//改變每頁(yè)顯示數(shù)據(jù)數(shù)量 le, ); }) </script> </body> </html>
效果如下:

Django
1,前端
<nav aria-label="Page navigation"> <div class="pagination"> <ul class="pagination"> {% if books.has_previous %} <a class='active' href="?page={{ books.previous_page_number }}">上一頁(yè)</a> {% endif %} <!-- {% for num in books.paginator.page_range%} {%if pindex == books.number%} <a href="">{{ num }}</a> {%else%} <a href="/books/borrow_show/{{num}}">{{ num }}</a> {%endif%} {% endfor %} --> <span class="current"> 第{{ books.number }}頁(yè)</span> {% if books.has_next %} <a class='active' href="?page={{ books.next_page_number }}">下一頁(yè)</a> {% endif %} 共{{ books.paginator.num_pages }}頁(yè) </ul> </div> </nav>
2,后端
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage, InvalidPage
class My_player(View):
@method_decorator(check_login)
def get(self,request):
res = request.COOKIES.get("token")
ret = Account.objects.filter(uuid=int(res)).first()
result = Friendscircle.objects.filter(accountid=ret.id).first()
#玩家信息
info = Friendscircleaccount.objects.filter(fcircleid=result.id).values('accountid')
id_list = []
for i in info:
if i['accountid'] != ret.id:
id_list.append(i['accountid'])
book_list = Account.objects.filter(id__in=id_list)
paginator = Paginator(book_list, 5)
page = request.GET.get('page')
try:
books = paginator.page(page)
# todo: 注意捕獲異常
except PageNotAnInteger:
# 如果請(qǐng)求的頁(yè)數(shù)不是整數(shù), 返回第一頁(yè)。
books = paginator.page(1)
except InvalidPage:
# 如果請(qǐng)求的頁(yè)數(shù)不存在, 重定向頁(yè)面
return HttpResponse('找不到頁(yè)面的內(nèi)容')
except EmptyPage:
# 如果請(qǐng)求的頁(yè)數(shù)不在合法的頁(yè)數(shù)范圍內(nèi),返回結(jié)果的最后一頁(yè)。
books = paginator.page(paginator.num_pages)
return render(request,'myPlayer.html', {'books': books})


浙公網(wǎng)安備 33010602011771號(hào)