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)

关键代码

相关推荐
mqiqe9 小时前
LLM Gateway:企业级大模型应用的“统一控制平面”
平面·flask·gateway
aGdF8E3gQ17 小时前
【Application Insights】采样率对Function App日志收集的影响和解决方法
python·flask·wpf
编码者卢布1 天前
【Azure APIM】通过 API Management 公开API为 MCP Server 的试验 (二)
microsoft·flask·azure
编码者卢布2 天前
【Azure APIM】通过 API Management 公开现有 MCP Server 的试验 (一)
microsoft·flask·azure
这个人懒得名字都没写3 天前
Flask + PyArmor Gunicorn启动报错:RuntimeError: unauthorised use of script
python·flask·gunicorn·pyarmor
立心者04 天前
Sdcb Chats .. 发布,彻底移除 Azure.AI.OpenAI 专用包
人工智能·flask·azure
昆曲之源_娄江河畔4 天前
Python如何安装flask, pymssql
开发语言·python·flask·pymssql
韭菜炒鸡肝天5 天前
【App Service】在Azure环境中如何查看App Service实例当前的网络连接情况呢?
microsoft·flask·azure
看昭奚恤哭5 天前
ontainer App】Container App无法从Container Registries 拉取镜像 - 报错 Forbidden
后端·python·flask
米码收割机5 天前
【python】Flask 贵州省兴仁市藠头价格数据分析和可视化系统(源码+文档)【独一无二】
python·数据分析·flask