28.1.帖子分页功能完成
(1)manage.py
通过命令生成100个帖子,用于测试
@manager.commanddef create_test_post(): for x in range(1,100): title='我是标题%s'%x content='我是内容,我的编号是%s'%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('测试帖添加成功')
(2)front/views.py
@bp.route('/')def index(): banners = BannerModel.query.order_by(BannerModel.priority.desc()).limit(4) boards = BoardModel.query.all() #获取当前页码数 page = request.args.get(get_page_parameter(), type=int, default=1) # 显示10条帖子 start = (page - 1) * config.PER_PAGE end = start + config.PER_PAGE posts = PostModel.query.slice(start, end) # bs_version=3:表示用Bootstrap v3版本 pagination = Pagination(bs_version=3,page=page,total=PostModel.query.count(), outer_window = 0, inner_window = 2) context = { 'banners':banners, 'boards':boards, 'posts':posts, 'pagination':pagination, } return render_template('front/front_index.html',**context)
(3)front_index/html
{ { pagination.links }}
28.2.板块过滤功能完成
(1)front_index.html
{% if current_board %} 所有版块 {% else %} 所有版块 {% endif %} {% for board in boards %} {% if board.id==current_board %} { { board.name }} {% else %} { { board.name }} {% endif %} {% endfor %}
(2)front/views.py
@bp.route('/')def index(): board_id = request.args.get('bd', type=int, default=None) # 获取当前页码数 page = request.args.get(get_page_parameter(), type=int, default=1) banners = BannerModel.query.order_by(BannerModel.priority.desc()).limit(4) boards = BoardModel.query.all() # 显示10条帖子 start = (page - 1) * config.PER_PAGE end = start + config.PER_PAGE posts = None total = 0 if board_id: query_obj = PostModel.query.filter_by(board_id=board_id) posts = query_obj.slice(start,end) total = query_obj.count() else: posts = PostModel.query.slice(start, end) total = PostModel.query.count() # bs_version=3:表示用Bootstrap v3版本 pagination = Pagination(bs_version=3,page=page,total=total,outer_window = 0, inner_window = 2) context = { 'banners':banners, 'boards':boards, 'posts':posts, 'pagination':pagination, 'current_board':board_id #把当前板块id传到前端,前端添加“active”样式 } return render_template('front/front_index.html',**context)