You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wagtail分页时保留文章分类参数的问题求助

Wagtail分类分页丢失category参数问题修复

问题

普通文章分页功能正常,但分类筛选后点击分页按钮,category参数会丢失,导致跳转回全量文章列表。

用户代码

模板代码(原)

{% if posts.paginator.num_pages > 1 %}
    {% if not 1 == posts.number and posts.number >= 4 %}
        {% if posts.paginator.num_pages > 5  %}
            <a href="?page=1">&lt;&lt;</a>
        {% endif %}
    {% endif %}
    
    {% for page_num in posts.paginator.page_range %}

        {% if posts.number == 1 %}
            {% if page_num > posts.number|add:-3  and page_num < posts.number|add:5 %}
                <a href="?page={{ page_num }}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}
        {% elif posts.number == 2 %}      
            {% if page_num > posts.number|add:-2  and page_num < posts.number|add:4 %}
                <a href="?page={{ page_num }}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}
        {% elif posts.number == 3 %}                            
            {% if page_num > posts.number|add:-3  and page_num < posts.number|add:3 %}
                <a href="?page={{ page_num }}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}
        {% elif posts.paginator.num_pages == posts.number %} 
            {% if page_num > posts.number|add:-5  and page_num < posts.number|add:3 %}
                <a href="?page={{ page_num }}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}
        {% elif posts.number == posts.paginator.num_pages|add:-1 %} 
            {% if page_num > posts.number|add:-4  and page_num < posts.number|add:2 %}
                <a href="?page={{ page_num }}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}   
        {% elif posts.number == posts.paginator.num_pages|add:-2 %} 
            {% if page_num > posts.number|add:-3  and page_num < posts.number|add:3 %}
                <a href="?page={{ page_num }}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}                                               
        {% else %}
            {% if page_num > posts.number|add:-3  and page_num < posts.number|add:3 %}
                <a href="?page={{ page_num }}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}     
        {% endif %}  

    {% endfor %}
    {% if not posts.paginator.num_pages == posts.number and posts.number <= posts.paginator.num_pages|add:-3 %}
        {% if posts.paginator.num_pages > 5  %}
            <a href="?page={{ posts.paginator.num_pages }}">&gt;&gt;</a>
        {% endif %}                    
    {% endif %}
{% endif %}

视图代码(原)

def get_context(self, request, *args, **kwargs):
    context = super().get_context(request, *args, **kwargs)

    if request.GET.get('category'):

        category_posts = BlogDetailPage.objects.live().public().filter(categories__slug__in=[request.GET.get('category')]).order_by('-first_published_at')
        paginator = Paginator(category_posts, 6)
        page = request.GET.get("page")
        try:
            posts = paginator.page(page)
        except PageNotAnInteger:
            posts = paginator.page(1)
        except EmptyPage:
            posts = paginator.page(paginator.num_pages)

        context["posts"] = posts

    else:
        all_posts = BlogDetailPage.objects.live().public().order_by('-first_published_at')
        paginator = Paginator(all_posts, 6)
        page = request.GET.get("page")
        try:
            posts = paginator.page(page)
        except PageNotAnInteger:
            posts = paginator.page(1)
        except EmptyPage:
            posts = paginator.page(paginator.num_pages)

        context["posts"] = posts
    return context

修复方案

1. 模板层:保留当前查询参数

问题核心是分页链接仅传递page参数,丢失了category。通过Django模板的request.GET.copy复制当前查询参数,修改page值后再生成URL,可自动保留所有现有参数。

修改所有分页链接的href部分:

  • 第一页链接:
    <a href="?{% with request.GET.copy as query %}{% query.set 'page' 1 %}{{ query.urlencode }}{% endwith %}">&lt;&lt;</a>
    
  • 分页数字链接:
    <a href="?{% with request.GET.copy as query %}{% query.set 'page' page_num %}{{ query.urlencode }}{% endwith %}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>
    
  • 最后一页链接:
    <a href="?{% with request.GET.copy as query %}{% query.set 'page' posts.paginator.num_pages %}{{ query.urlencode }}{% endwith %}">&gt;&gt;</a>
    

修改后的完整模板代码:

{% if posts.paginator.num_pages > 1 %}
    {% if not 1 == posts.number and posts.number >= 4 %}
        {% if posts.paginator.num_pages > 5  %}
            <a href="?{% with request.GET.copy as query %}{% query.set 'page' 1 %}{{ query.urlencode }}{% endwith %}">&lt;&lt;</a>
        {% endif %}
    {% endif %}
    
    {% for page_num in posts.paginator.page_range %}

        {% if posts.number == 1 %}
            {% if page_num > posts.number|add:-3  and page_num < posts.number|add:5 %}
                <a href="?{% with request.GET.copy as query %}{% query.set 'page' page_num %}{{ query.urlencode }}{% endwith %}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}
        {% elif posts.number == 2 %}      
            {% if page_num > posts.number|add:-2  and page_num < posts.number|add:4 %}
                <a href="?{% with request.GET.copy as query %}{% query.set 'page' page_num %}{{ query.urlencode }}{% endwith %}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}
        {% elif posts.number == 3 %}                            
            {% if page_num > posts.number|add:-3  and page_num < posts.number|add:3 %}
                <a href="?{% with request.GET.copy as query %}{% query.set 'page' page_num %}{{ query.urlencode }}{% endwith %}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}
        {% elif posts.paginator.num_pages == posts.number %} 
            {% if page_num > posts.number|add:-5  and page_num < posts.number|add:3 %}
                <a href="?{% with request.GET.copy as query %}{% query.set 'page' page_num %}{{ query.urlencode }}{% endwith %}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}
        {% elif posts.number == posts.paginator.num_pages|add:-1 %} 
            {% if page_num > posts.number|add:-4  and page_num < posts.number|add:2 %}
                <a href="?{% with request.GET.copy as query %}{% query.set 'page' page_num %}{{ query.urlencode }}{% endwith %}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}   
        {% elif posts.number == posts.paginator.num_pages|add:-2 %} 
            {% if page_num > posts.number|add:-3  and page_num < posts.number|add:3 %}
                <a href="?{% with request.GET.copy as query %}{% query.set 'page' page_num %}{{ query.urlencode }}{% endwith %}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}                                               
        {% else %}
            {% if page_num > posts.number|add:-3  and page_num < posts.number|add:3 %}
                <a href="?{% with request.GET.copy as query %}{% query.set 'page' page_num %}{{ query.urlencode }}{% endwith %}" class="{% if page_num == posts.number %} active {% endif %}">{{ page_num }}</a>    
            {% endif %}     
        {% endif %}  

    {% endfor %}
    {% if not posts.paginator.num_pages == posts.number and posts.number <= posts.paginator.num_pages|add:-3 %}
        {% if posts.paginator.num_pages > 5  %}
            <a href="?{% with request.GET.copy as query %}{% query.set 'page' posts.paginator.num_pages %}{{ query.urlencode }}{% endwith %}">&gt;&gt;</a>
        {% endif %}                    
    {% endif %}
{% endif %}

2. 视图层:简化重复代码(可选)

原视图中分类和全量文章的分页逻辑重复,可合并优化:

def get_context(self, request, *args, **kwargs):
    context = super().get_context(request, *args, **kwargs)
    
    # 基础查询集
    queryset = BlogDetailPage.objects.live().public().order_by('-first_published_at')
    
    # 处理分类过滤
    category_slug = request.GET.get('category')
    if category_slug:
        queryset = queryset.filter(categories__slug__in=[category_slug])
    
    # 统一分页逻辑
    paginator = Paginator(queryset, 6)
    page = request.GET.get("page")
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)
    
    context["posts"] = posts
    # 可选:传递当前分类到模板,用于高亮等逻辑
    context["current_category"] = category_slug
    return context

内容的提问来源于stack exchange,提问作者Elrigor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 06:25:19