Flask實戰第60天:文章分頁技術實現

來源:互聯網
上載者:User

標籤:翻頁   board   實現   def   圖片   ati   使用   http   parameter   

編輯manage.py,添加測試文章

@manager.commanddef create_test_post():    for x in range(1, 100):        title = ‘標題{}‘.format(x)        content = ‘內容:{}‘.format(x)        board = BoardModel.query.first()        author = FrontUser.query.first()        post = PostModel(title=title, content=content)        post.board = board        post.author = author        db.session.add(post)        db.session.commit()    print(‘測試文章添加成功‘)

運行

python manage.py create_test_post

在 flask架構中,我們可以使用Flask Paginate外掛程式來實現分頁

https://pythonhosted.org/Flask-paginate/

安裝外掛程式

pip install flask-paginate

編輯配config.py,配置每頁顯示的貼文數

#flask-paginate的相關配置PER_PAGE = 6   #每頁顯示6篇文章

編輯首頁的視圖函數,編輯front.views.py

...from flask_paginate import Pagination, get_page_parameter#get_page_parameter可以擷取到當前頁

現在重新整理首頁只會顯示6篇文章了

實現翻頁

編輯front_index.html,在文章下面加上

重新整理頁面,發現沒有樣式

解決這個問題,需要在執行個體化Pagination加上指定 bootstrap版本的參數即可

Flask實戰第60天:文章分頁技術實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.