V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
honmaple
V2EX  ›  Flask

分享一段 flask 分页代码

  •  
  •   honmaple · 2016-05-29 10:48:34 +08:00 · 3064 次点击
    这是一个创建于 2882 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用了 bootstrap 的分页 class ,具体看gist

    使用示例

    topics = Topic.query.paginate(page,app.config['PER_PAGE'],error_out=True)
    

    templates

    {% from 'paginate.html' import paginate %}
    {{ paginate(topics,'topic.topic') }}
    

    如果含参数

    {{ paginate(topics,'topic.topic',dict(tag=tag) }}
    

    效果 honmaple.cn/paginate

    10 条回复    2016-06-02 00:28:44 +08:00
    nowbear
        1
    nowbear  
       2016-05-29 10:59:02 +08:00 via Android
    感谢分享
    Akarin
        2
    Akarin  
       2016-05-29 12:29:52 +08:00
    亮点在那里?
    ibigbug
        3
    ibigbug  
       2016-05-29 14:58:43 +08:00
    每页展示 38 条怎么办?
    honmaple
        4
    honmaple  
    OP
       2016-05-29 15:24:54 +08:00
    @nowbear 就一般的分页
    honmaple
        5
    honmaple  
    OP
       2016-05-29 15:25:14 +08:00
    @ibigbug PER_PAGE = 38
    ibigbug
        6
    ibigbug  
       2016-05-30 12:19:47 +08:00   ❤️ 1
    要重新部署?
    honmaple
        7
    honmaple  
    OP
       2016-05-30 12:24:36 +08:00   ❤️ 1
    改一下就行,用不了几分钟,使用 PER_PAGE 的话以后容易修改,你也可以直接

    topics = Topic.query.paginate(page,38,error_out=True)
    ibigbug
        8
    ibigbug  
       2016-05-31 20:22:19 +08:00
    应该写到 querystring 由用户传进来
    honmaple
        9
    honmaple  
    OP
       2016-05-31 20:55:34 +08:00
    @ibigbug 你是说将每页多少作为用户设置存入数据库,还是 session
    ibigbug
        10
    ibigbug  
       2016-06-02 00:28:44 +08:00
    `def pager(page=1, per_page=10):`
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3642 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 04:21 · PVG 12:21 · LAX 21:21 · JFK 00:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.