Python之tornado框架實現翻頁功能

一、結果如圖所示,這裏將html頁面與網站的請求處理放在不一樣地方了html

 

start.py代碼前端

import tornado.ioloop
import tornado.web
from controllers import home

settings = {

    'static_path':'statics',#靜態文件配置,須要特殊處理
    'static_url_prefix':'/sss/',#標記文件開始的名字
}


#路由映射,根據不一樣url對應到不一樣的類裏面
application = tornado.web.Application([
    (r"/index/(?P<page>\d*)", home.IndexHandler),
],**settings)#基於正則的路由,?P<page>表示爲正則匹配到的內容起了一個名字


if __name__=='__main__':
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

  home.py裏面的代碼python

import tornado.web

List_Info=[
    {'username':'jay','email':'810833835@qq.com'}
]

class IndexHandler(tornado.web.RequestHandler):
    def get(self,page):

        #每頁顯示五條數據
        #page是當前頁
        #第一頁: 0:5 List_Info[0:5]
        #第二頁:5:10
        #start: (page-1)*5
        #end:   (page)*5

        try:page=int(page)
        except:page=1
        print(page)
        if page <1:
            page =1
        start = (page - 1)*5
        end = page * 5
        current_ist = List_Info[start:end]

        self.render('E:\\練習2\\tornado_day2\\views\\home\\index.html',list= current_ist,page=page)
#E:\\練習2\\tornado_day2\\views\\home\\index.html
    def post(self,page):
        user = self.get_argument('username')
        email = self.get_argument('email')
        temp = {'username':user,'email':email}
        List_Info.append(temp)
        self.redirect('/index/'+page)

  home文件裏面的index.html代碼web

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index首頁</title>
</head>
<body>
<h1>提交數據</h1>
<form method="post" action="/index/{{page}}">
    <input name="username" type="text">
    <input name="email" type="text">
    <input value="提交" type="submit">
</form>
<h1>顯示數據</h1>
<table border="2">
    <thead><tr>
        <th>用戶名</th>
        <th>郵箱</th>
    </tr></thead>
    <tbody>
    {% for line in list %}
    <tr>
        <td>{{line['username']}}</td>
        <td>{{line['email']}}</td>
    </tr>
    {% end %}
    </tbody>
</table>
</body>
</html>

  運行結果如圖app

最後咱們修改一下home裏面的代碼,實現選擇頁面功能tornado

import tornado.web

List_Info=[
    {'username':'jay','email':'810833835@qq.com'}
]

for i in range(1000):
    data = {'username': i, 'email': i * 2}
    List_Info.append(data)
class IndexHandler(tornado.web.RequestHandler):
    def get(self,page):

        #每頁顯示五條數據
        #page是當前頁
        #第一頁: 0:5 List_Info[0:5]
        #第二頁:5:10
        #start: (page-1)*5
        #end:   (page)*5

        try:page=int(page)
        except:page=1

        if page <1:
            page =1
        start = (page - 1)*5
        end = page * 5#每頁最多顯示五條數據
        current_ist = List_Info[start:end]
        all_page ,c = divmod(len(List_Info),5)#每五條數據一頁,判斷多少頁
        if c>0:#有餘數則多一頁
            all_page +=1
        list_page =[]
        print(all_page)
        if page <5:
            for p in range(9):
                if p+1 == page:
                    temp = '<a class="active" href="/index/%s">%s</a>' % (p+1,p+1)
                else:
                    temp = '<a href="/index/%s">%s</a>' % (p + 1, p + 1)
                list_page.append(temp)
        elif page >all_page-5:#判斷頁數是否在最前或者最後五頁

            for p in range(all_page-9,all_page):
                if p + 1 == page:
                    temp = '<a class="active" href="/index/%s">%s</a>' % (p + 1, p + 1)
                else:
                    temp = '<a href="/index/%s">%s</a>' % (p + 1, p + 1)
                list_page.append(temp)
        else:
            for p in range(page-5,page+4):
                if p+1 == page:
                    temp = '<a class="active" href="/index/%s">%s</a>' % (p+1,p+1)
                else:
                    temp = '<a href="/index/%s">%s</a>' % (p + 1, p + 1)
                list_page.append(temp)
        str_page = ''.join(list_page)
        # str_page = list_page,不寫join則會在頁面上顯示逗號

        self.render('E:\\練習2\\tornado_day2\\views\\home\\index.html',list= current_ist,page=page,str_page=str_page)
#E:\\練習2\\tornado_day2\\views\\home\\index.html
    def post(self, page):
        user = self.get_argument('username')
        email = self.get_argument('email')
        temp = {'username':user,'email':email}
        List_Info.append(temp)
        self.redirect('/index/'+page)
        print(List_Info)

  對於index.html文件也要作少少修改oop

最後把home裏面的代碼封裝一下,如圖post

import tornado.web

List_Info=[
    {'username':'jay','email':'810833835@qq.com'}
]

for i in range(1000):
    data = {'username': i, 'email': i * 2}
    List_Info.append(data)#認爲建立1000條數據

class Pagination:
    def __init__(self,current_page,all_item):
        all_page, c = divmod(len(all_item), 5)  # 每五條數據一頁,判斷多少頁
        self.all_page=all_page

        if c > 0:  # 有餘數則多一頁
            all_page += 1

        self.all_page = all_page#獲取最大頁面
        try:
            current_page=int(current_page)
        except:
            current_page=1

        if current_page <1:
            current_page =1

        self.current_page = current_page

    @property
    def strat(self):
        return (self.current_page-1)*5

    @property
    def end(self):
        return self.current_page * 5


    def get_page(self,base_url):
        self.all_page, c = divmod(len(List_Info), 5)  # 每五條數據一頁,判斷多少頁
        if c > 0:  # 有餘數則多一頁
            self.all_page += 1

        list_page = []

        if self.current_page < 5:
            for p in range(9):
                if p + 1 == self.current_page:
                    temp = '<a class="active" href="%s %s">%s</a>' % (base_url,p + 1, p + 1)
                else:
                    temp = '<a href="%s%s">%s</a>' % (base_url,p + 1, p + 1)
                list_page.append(temp)
        elif self.current_page > self.all_page - 5:  # 判斷頁數是否在最前或者最後五頁

            for p in range(self.all_page - 9, self.all_page):
                if p == self.current_page:
                    temp = '<a class="active" href="%s %s">%s</a>' % (base_url, p , p )
                else:
                    temp = '<a href="%s%s">%s</a>' % (base_url, p, p )
                list_page.append(temp)
        else:
            for p in range(self.current_page - 5, self.current_page + 4):
                if p  == self.current_page:
                    temp = '<a class="active" href="%s %s">%s</a>' % (base_url, p , p)
                else:
                    temp = '<a href="%s%s">%s</a>' % (base_url, p , p )
                list_page.append(temp)
        str_page = ''.join(list_page)
        return str_page
        # str_page = list_page,不寫join則會在頁面上顯示逗號


class IndexHandler(tornado.web.RequestHandler):
    def get(self,page):

        page_obj =Pagination(page,List_Info)#把當前訪問頁以及要傳給前端的數據傳給類裏面

        current_ist =List_Info[page_obj.strat:page_obj.end]#以訪問字段的方式訪問方法,獲取數據
        str_page = page_obj.get_page('/index/')#底下的頁數碼

        self.render('E:\\練習2\\tornado_day2\\views\\home\\index.html',list= current_ist,page=page_obj.current_page,str_page=str_page)
#page_obj.current_page表示把處理過的頁數傳進去
    def post(self, page):
        user = self.get_argument('username')
        email = self.get_argument('email')
        temp = {'username':user,'email':email}
        List_Info.append(temp)
        self.redirect('/index/'+page)
        print(List_Info)

  運行如圖網站

相關文章
相關標籤/搜索