honmaple
V2EX  ›  Flask

分享一段 flask 分页代码

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

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