我正在尝试在我的列表视图页面上添加一个搜索栏。它将第一次获取所有帖子项目,如果查询放入搜索框并提交,它将返回过滤后的查询集。它渲染得很好,但只有分页有问题。对于未过滤的查询集,下一页将毫无问题地获得接下来的两个帖子,但对于过滤的查询集,我可以看到查询集通过查看更少的页面正确反映,但下一页让我获得了未过滤的第二页查询集不是过滤的查询集。谁能给我一些关于我在这里做错了什么的指示。谢谢
我的模板如下所示:
{% block content %}
  .....
 <form action="" method="get">
            <input type="text" name='q'>
            <input type="submit" value="Search">
     </form>
    {% for post in object_list %}
    ....
    {% endfor %}
    {% if is_paginated %}
            <div class="pagination">
                <span class="page-links">
                    {% if page_obj.has_previous %}
                        <a href="?page={{ page_obj.previous_page_number }}"><<</a>
                    {% endif %}
                    <span class="page-current">
                        Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
                    </span>
                    {% if page_obj.has_next %}
                        <a href="?page={{ page_obj.next_page_number }}">>></a>
                    {% endif %}
                </span>
            </div>
{% endif %}
我有一个如下所示的列表视图。
class Postlist(ListView):
model=post    
paginate_by = 2
query_string = ''
def get_queryset(self):
    if ('q' in self.request.GET) and self.request.GET['q'].strip():
       query_string = self.request.GET['q']
       entry_query = get_query(query_string, ['title', 'body',]) ## call get_query() function
       queryset = post.objects.filter(entry_query).order_by('-created')
    else:
       queryset=post.objects.all().order_by('-created')
    return queryset
def get_context_data(self):
    context = super(ListView, self).get_context_data(**kwargs)
    context['q'] = query_string
    ## do sth here to pre-populate the input text box
    return context