V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Tornado Documentation
http://www.v2ex.com/tornado/
Tornado on GitHub
https://github.com/facebook/tornado/
Tornado Gists
http://tornadogists.org/
waynee
V2EX  ›  Tornado

Tornado 分页问题求解!

  •  
  •   waynee · 2015-05-11 14:12:52 +08:00 · 4606 次点击
    这是一个创建于 3274 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近在学习Tornado,在写分页的时候遇到了瓶颈。
    是参照这个问答进行写的:
    http://stackoverflow.com/questions/15981257/can-tornado-handle-pagination

    路径就是:(r"/",MainHandler)

    逻辑是:
    class MainHandler(BaseHandler):
    def get(self,template_variables = {}):
    news_timeline = self.news_model.get_all_news() #获取所有news
    current_page = int(self.get_argument('page',0)) # 获取当前URL里的page参数
    template_variables["news_timeline"] = news_timeline
    template_variables["current_page"] = current_page
    self.render("index.html",**template_variables)

    HTMl是:
    <body>
    {% for news in news_timeline %}
    <a href = "{{ news["news_link"] }}">{{ news["news_name"] }}</a>
    {% end %}
    {% module Paginator(current_page,5, len(news_timeline)) %}
    </body>

    但是底部的分页链接能够显示出来,可是所有结果都显示在了一个页面,并没有分开显示。现在找不到问题在哪里,是我少些了什么代码吗?

    各位大神,求教!!!
    8 条回复    2015-05-12 13:25:23 +08:00
    waynee
        1
    waynee  
    OP
       2015-05-11 14:46:21 +08:00
    自己顶,搞了两天的,还是没效果。
    tpircsboy
        2
    tpircsboy  
       2015-05-11 14:57:28 +08:00 via Android   ❤️ 1
    tornado并不能帮你处理分页,分页整个逻辑都需要自己来做。而且stackoverflow上那个答案也说了他的代码只是用来生成分页连接,this doesn't limit those results for you, if just builds the pagination links。
    所以你需要把你的news_timeline自己分开。
    waynee
        3
    waynee  
    OP
       2015-05-11 15:00:33 +08:00
    @tpircsboy 那要在数据库查询的时候 使用 limit?没做过这方面的,求指教。
    zuroyu
        4
    zuroyu  
       2015-05-11 15:01:40 +08:00
    news_timeline = self.news_model.get_all_news() #获取所有news
    这不是所有结果才叫奇怪呢...
    Paginator只是实现了展示内容,并没有实现分页功能

    建议从简单开始写一个分页功能,搞清楚分页的原理。包含页数是如何算出来的, 如何获取分页内容, 真假分页的区别等...
    tpircsboy
        5
    tpircsboy  
       2015-05-11 15:17:53 +08:00 via Android
    @waynee 嗯,数据库查询时需要skip和limit
    waynee
        6
    waynee  
    OP
       2015-05-11 16:18:51 +08:00
    @tpircsboy skip 是干吗的?我只用了 limit 也实现了
    coderwang
        7
    coderwang  
       2015-05-11 16:37:38 +08:00   ❤️ 1
    @waynee 就是从第几条开始 mongodb 查询是skip limit , mysql 一个limit 就可以
    phithon
        8
    phithon  
       2015-05-12 13:25:23 +08:00
    你代码并没有分页,而且tornado里也并没有分页的辅助类、方法,分页自己实现。
    可以参考我写的Minos里的分页函数:
    https://github.com/phith0n/Minos
    主要是你后端得进行limit 限制每次查出来的是从几到几个对象。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4087 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 05:22 · PVG 13:22 · LAX 22:22 · JFK 01:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.