首页 > 解决方案 > 搜索后分页的第二页在 django 中不起作用

问题描述

搜索结果没有出现在第二页我应该改变什么来解决我的问题?我使用 elasticsearch 作为搜索引擎 index.html

     <ul>
                        {% for i in paginator.page_range %}
                              {% if i <= page_number|add:5 and i >= page_number|add:-5 %}

                                 <li class=" {% if i == page_number %} active {% endif %} " >
                                   <a href="?page={{forloop.counter}}">{{forloop.counter}}</a>
                                 </li>

                              {% endif %}

                        {% endfor %}
                          </ul>

这是我的views.py


def index(request):
    q = request.GET.get('q')
    if q:
        articles = PostDocument.search().query("match", title=q, )
        paginator = Paginator(articles, 5)
        page_number = request.GET.get('page', 1)
        page_obj = paginator.get_page(page_number)

        return render(request, 'index.html', {
            'articles': page_obj.object_list,
            'paginator': paginator,
            'page_number': int(page_number),

        })

    else:
        articles = ''
        return render(request, 'index.html', {'articles': articles})


标签: djangotemplateselasticsearchviewpagination

解决方案



推荐阅读