flask分页宏增加更多参数

背景:我正在开发一个博客,核心的两个model是文章和文章类别。

现在想要实现的功能是:点击一个文章类别,以分页的形式显示这个文章类别下的所有文章,类似这种效果。

参考的书中分页宏只接受页数这一个参数,经过尝试,成功给分页宏添加了文章类别参数,实现了上述功能。

posts.html

html 复制代码
{% extends "public/base.html" %}
{% import "public/macros.html" as macros %}

{% block content %}
    <div class="row" style="margin-top: 20px;">
        <div class="col-sm-4">
            {% include "public/categories.html" %}
        </div>
        {% if posts %}
            <div class="col-sm-8">
                {% include "public/_posts.html" %}
            <div style="text-align: center; margin-top: 20px;">
                {% if category %}
                    {{  macros.pagination_widget(pagination, '.posts', category.id) }}
                {% else %}
                    {{  macros.pagination_widget(pagination, '.posts', 0) }}
                {% endif %}
            </div>
        </div>
        {% endif %}
    </div>
{% endblock %}

关键代码

macros.html

python 复制代码
{% macro pagination_widget(pagination, endpoint, category_id) %}
        <ul class="pagination justify-content-center">
            <li {% if not pagination.has_prev %} class="page-item disabled" {% else %} class="page-item" {% endif %}>
                <a class="page-link" href="{% if pagination.has_prev %}{{ url_for(endpoint,
                page = pagination.page - 1, category_id = category_id, **kwargs) }}{% else %}#{% endif %}">
                Previous
                </a>
            </li>
                    {% for p in pagination.iter_pages() %}
                        {% if p %}
                            {% if p == pagination.page %}
                            <li class="page-item active">
                            <a class="page-link" href="{{ url_for(endpoint, page = p, category_id = category_id, **kwargs) }}">{{ p }}</a>
                            </li>
                            {% else %}
                            <li class="page-item">
                            <a class="page-link" href="{{ url_for(endpoint, page = p, category_id = category_id, **kwargs) }}">{{ p }}</a>
                            </li>
                            {% endif %}
                        {% else %}
                        <li class="page-item disabled"><a class="page-link" href="#">...</a></li>
                        {% endif %}
                    {% endfor %}
            <li {% if not pagination.has_next %} class="disabled page-item" {% else %} class="page-item"{% endif %}>
                <a class="page-link" href="{% if pagination.has_next %}{{ url_for(endpoint,
                page = pagination.page + 1, category_id = category_id, **kwargs) }}{% else %}#{% endif %}">
                Next
                </a>
            </li>
        </ul>
{% endmacro %}

关键代码

routes.py

python 复制代码
@app.route('/posts', methods=['GET', 'POST'])
@app.route('/posts/<int:category_id>', methods=['GET', 'POST'])
def posts(category_id=0):
    page = request.args.get('page', 1, type=int)
    if 0 != category_id:
        category = Category.query.get_or_404(category_id)
        pagination = Post.query.with_parent(category).order_by(Post.timestamp.desc()).paginate(page=page, per_page=10)
    else:
        category = None
        pagination = Post.query.order_by(Post.timestamp.desc()).paginate(page=page, per_page=10)
    posts = pagination.items
    categories = Category.query.all()
    return render_template('public/posts.html', posts=posts, pagination=pagination, categories=categories, category=category)

关键代码

相关推荐
高级程序源5 天前
django招聘网站信息爬取与分析系统79704-计算机课程设计、毕业设计
后端·python·mysql·小程序·django·flask·课程设计
lpfasd1236 天前
配置文件格式对比 与 AI Coding 的选择逻辑
python·flask·numpy
troy1287 天前
Python 基础语法(九):Django/Flask/FastAPI 三大 Web 框架详细对比解析
python·jupyter·django·flask·github·fastapi
小白学大数据7 天前
Python 项目实战:用 Flask 构建生产级 MySQL 增删改查 REST API
开发语言·人工智能·python·mysql·flask
典典分享指南7 天前
用飞书 Wiki 搭内容作战室:从零搭建团队内容协作中枢
django·flask·numpy·pyqt
life码农8 天前
python框架Flask多语言配置示例
python·flask
毕业设计70310 天前
(免费领源码)基于Python的博物馆研学活动管理系统的设计与实现22724- java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
python·mysql·随机森林·pycharm·django·flask·推荐算法
深圳市尚想信息技术有限公司10 天前
HITENX海速芯TM52F1363-SSOP24 8KB Flash存储+EEPROM,国产8位MCU单片机
单片机·mcu·flask·芯片·控制器·海速芯
李高钢12 天前
Python Flask 框架入门:从零搭建你的第一个 Web 应用
前端·python·flask
半亩码田13 天前
【.NET新特性·第17篇】EF Core 10 与 Aspire 13.1
python·flask·.net