honmaple
V2EX  ›  Flask

分享一段 flask 分页代码

  •  
  •   honmaple · May 29, 2016 · 3754 views
    This topic created in 3637 days ago, the information mentioned may be changed or developed.

    使用了 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 replies    2016-06-02 00:28:44 +08:00
    nowbear
        1
    nowbear  
       May 29, 2016 via Android
    感谢分享
    Akarin
        2
    Akarin  
       May 29, 2016
    亮点在那里?
    ibigbug
        3
    ibigbug  
       May 29, 2016
    每页展示 38 条怎么办?
    honmaple
        4
    honmaple  
    OP
       May 29, 2016
    @nowbear 就一般的分页
    honmaple
        5
    honmaple  
    OP
       May 29, 2016
    @ibigbug PER_PAGE = 38
    ibigbug
        6
    ibigbug  
       May 30, 2016   ❤️ 1
    要重新部署?
    honmaple
        7
    honmaple  
    OP
       May 30, 2016   ❤️ 1
    改一下就行,用不了几分钟,使用 PER_PAGE 的话以后容易修改,你也可以直接

    topics = Topic.query.paginate(page,38,error_out=True)
    ibigbug
        8
    ibigbug  
       May 31, 2016
    应该写到 querystring 由用户传进来
    honmaple
        9
    honmaple  
    OP
       May 31, 2016
    @ibigbug 你是说将每页多少作为用户设置存入数据库,还是 session
    ibigbug
        10
    ibigbug  
       Jun 2, 2016
    `def pager(page=1, per_page=10):`
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5530 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 07:43 · PVG 15:43 · LAX 00:43 · JFK 03:43
    ♥ Do have faith in what you're doing.