BBS论坛(二十八)
2025-01-15 02:41:42

LIKE.TG 成立于2020年,总部位于马来西亚,是首家汇集全球互联网产品,提供一站式软件产品解决方案的综合性品牌。唯一官方网站:www.like.tg
28.1.帖子分页功能完成
(1)manage.py
通过命令生成100个帖子,用于测试
@manager.command
def 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
<div style="text-align: center">
{{ pagination.links }}
</div>
28.2.板块过滤功能完成
(1)front_index.html
<div class="sm-container">
<div style="margin-bottom: 10px;">
<a class="btn btn-warning btn-block" href="{{ url_for('front.apost') }}" id="add-post-btn">发布帖子</a>
</div>
<div class="list-group">
{% if current_board %}
<a href="/" class="list-group-item">所有版块</a>
{% else %}
<a href="/" class="list-group-item active">所有版块</a>
{% endif %}
{% for board in boards %}
{% if board.id==current_board %}
<a href="{{ url_for('front.index',bd=board.id) }}"
class="list-group-item active">{{ board.name }}</a>
{% else %}
<a href="{{ url_for('front.index',bd=board.id) }}" class="list-group-item">{{ board.name }}</a>
{% endif %}
{% endfor %}
</div>
</div>
(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)

现在关注【LIKE.TG出海指南频道】、【LIKE.TG生态链-全球资源互联社区】,即可免费领取【WhatsApp、LINE、Telegram、Twitter、ZALO云控】等获客工具试用、【住宅IP、号段筛选】等免费资源,机会难得,快来解锁更多资源,助力您的业务飞速成长!点击【联系客服】
本文由LIKE.TG编辑部转载自互联网并编辑,如有侵权影响,请联系官方客服,将为您妥善处理。
This article is republished from public internet and edited by the LIKE.TG editorial department. If there is any infringement, please contact our official customer service for proper handling.