標籤:翻頁 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天:文章分頁技術實現