如何在web.pypython中使用列表中的结果实现分页



我使用python web.py来设计一个小型web应用程序,实际上我没有使用任何数据库来获取结果/记录,我会有一个记录列表(根据要求,我会从一些地方获得:)

下面是我的代码

code.py

import web
from web import form
urls = (
    '/', 'index',
    '/urls', 'urls_result',
)
app =  web.application(urls, globals())
render = web.template.render('templates/')
class index:
    def GET(self):
        return render.home()
    def POST(self):
        result_list = [('Images', 'http://www.google.co.in/imghp?hl=en&tab=wi'), 
                       ('Maps', 'http://maps.google.co.in/maps?hl=en&tab=wl'), 
                       ('Play', 'https://play.google.com/?hl=en&tab=w8'), 
                       ('YouTube', 'http://www.youtube.com/?gl=IN&tab=w1'), 
                       ('News', 'http://news.google.co.in/nwshp?hl=en&tab=wn'),  
                       ('Gmail', 'https://mail.google.com/mail/?tab=wm'), 
                       ('Drive', 'https://drive.google.com/?tab=wo'), 
                       ('More»', 'http://www.google.co.in/intl/en/options/'), 
                       ('Web History', 'http://www.google.co.in/history/optout?hl=en'), 
                       ('Settings', 'http://www.google.co.in/preferences?hl=en'), 
                       ('Sign in', 'https://accounts.google.com/ServiceLogin?hl=en&continue=http://www.google.co.in/'), 
                       ('Advanced search', 'http://www.google.co.in/advanced_search?hl=en-IN&authuser=0'),
                       .............. 
                       ..............
                       .............. so on until 200 records      ]
        return render.recordslist(result_list)
if __name__ == "__main__":
    app.run()

home.html

$def with()
<html>
 <head>
   <title>Home Page</title>
  <body alink="green" link="blue" >
    <div class="main">
      <center>
              <form method="POST" action='urls'>
                  <input class="button" type="submit" name="submit" value="Submit" />
              </form>
      </center>
     </div>
  </body>
</html>

recordslist.html

$def with(result_list)
<html>
 <head>
    <title>List of records</title> 
 </head>
 <body> 
   <table>
     $for link in result_list:
     <tr>
        <td>$link[0]</td>
        <td>$link[1]</td>
     </tr>    
   </table> 
 </body>

因此,从上面的代码中,我所做的是,当我运行服务器并用web.py返回的ip访问浏览器时,它会被重定向到主页(url为/,模板为home.html),主页由一个按钮组成。

现在,我不使用任何database来获取记录,只是我有硬拷贝的记录,它们的形式是list of tuples,正如您在上面看到的那样。

因此,当用户点击提交按钮i,通过指向呈现模板recordslist.html/url,以table的形式显示记录时

现在上述过程运行良好。但这里的list of tuples/records可能高达200 or more,所以我想为/url页面实现pagination

我在谷歌上搜索了很多,所有的点击都是为了从数据库中检索记录,但不是从列表中检索,我真的很困惑如何用10 pages for page对结果进行分页。

所以有人能告诉我现在如何从上面的代码列表中对结果/记录进行分页吗。

获取页面

首先,您必须从用户的请求中提取页面。假设您将使用页面查询字符串参数,则可以使用该参数来确定页码:

params = web.input()
page = params.page if hasattr(params, 'page') else 1

使用页面

一旦有了一个页面,分页所涉及的就是返回一部分结果。以下函数应该为您提供所需的切片(假设页面为1索引):

def get_slices(page, page_size=10):
    return (page_size * (page - 1), (page_size * page)

这将返回可用于分割结果列表的下限和上限。因此,在当前return render.recordslist(result_list)的位置,您可以使用:

lower, upper = get_slices(page)
return render.recordslist(result_list[lower:upper])

最新更新