1

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

下面是我的代码

代码.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()

主页.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>

记录列表.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>

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

现在在这里我没有使用任何database来获取记录,只是我有硬核记录,其形式list of tuples如您在上面看到的那样。

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

现在上述过程工作正常。但是这里list of tuples/records可能最多200 or more,所以我想实现pagination页面/url

我用谷歌搜索了很多,所有的命中都是为了从数据库中检索记录,但不是从列表中,我真的很困惑如何用10 pages for page.

那么现在谁能告诉我如何从上面的代码中对列表中的结果/记录进行分页。

4

1 回答 1

1

Get the Page

Firstly, you would have to pull the page out of the request from the user. Assuming that you will use a page querystring parameter, you can use this to determine the page number:

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

Use the Page

Once you have a page, all that pagination involves is returning a slice of results. The following function should give you the slices required (assuming that pages are 1-indexed):

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

This will return the lower and upper bound that you can use to slice your results list. So where you currently return render.recordslist(result_list), you could instead use:

lower, upper = get_slices(page)
return render.recordslist(result_list[lower:upper])
于 2013-02-21T13:07:46.850 回答