【发布时间】:2013-11-03 21:39:13
【问题描述】:
我正在尝试在我的列表视图页面上添加一个搜索栏。它将第一次获取所有帖子项目,如果查询放入搜索框并提交,它将返回过滤后的查询集。它渲染得很好,但只有分页有问题。对于未过滤的查询集,下一页将毫无问题地获得接下来的两个帖子,但对于过滤的查询集,我可以看到查询集通过查看更少的页面正确反映,但下一页让我获得了未过滤的第二页查询集不是过滤的查询集。谁能给我一些关于我在这里做错了什么的指示。谢谢
我的模板如下所示:
{% 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
【问题讨论】:
-
实际上我弄清楚了问题所在。因为 get_query() 是懒惰的。 next page 函数实际上会向数据库发起另一个查询以获取接下来的两个项目,因为当它查询时,我的“q”没有任何搜索词,所以逻辑转到“else”部分以获取所有帖子。我认为覆盖 get_context_data() 以在每次回发期间保留搜索词将解决问题。
标签: python django listview pagination