英文:
Dajngo: Pagination in search results with Class-based views
问题
我想要根据我在表单中输入的关键词进行分页。
我使用以下类
def pageNotFound(request, exceprion):
return HttpResponseNotFound("<h2>Page not found</h2>")
def get_quotes():
top_tags = get_top_tags()
context = {
"top_tags": top_tags,
"functional_menu": functional_menu,
}
return context
class Main(ListView):
model = Quote
paginate_by = 10
template_name = "quotes/index.html"
context_object_name = "quotes"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update(get_quotes())
return context
class SearchedResults(ListView):
model = Quote
paginate_by = 10
template_name = "quotes/index.html"
context_object_name = "quotes"
def get_queryset(self):
query = self.request.GET.get("search_query")
if query:
queryset = Quote.objects.filter(
Q(quote__icontains=query)
| Q(tags__name__icontains=query)
| Q(author__fullname__icontains=query)
).distinct()
else:
queryset = super().get_queryset()
return queryset
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
问题是,当您跳转到下一页时,表单字段被清空,查询变成了 None query=None
。整个分页变得未定义。如何在分页时保存查询集(queryset)?
英文:
I want to paginate by keywords that I enter in the form.
I use the following class
def pageNotFound(request, exceprion):
return HttpResponseNotFound("<h2>Page not found</h2>")
def get_quotes():
top_tags = get_top_tags()
context = {
"top_tags": top_tags,
"functional_menu": functional_menu,
}
return context
class Main(ListView):
model = Quote
paginate_by = 10
template_name = "quotes/index.html"
context_object_name = "quotes"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update(get_quotes())
return context
class SearchedResults(ListView):
model = Quote
paginate_by = 10
template_name = "quotes/index.html"
context_object_name = "quotes"
def get_queryset(self):
query = self.request.GET.get("search_query")
if query:
queryset = Quote.objects.filter(
Q(quote__icontains=query)
| Q(tags__name__icontains=query)
| Q(author__fullname__icontains=query)
).distinct()
else:
queryset = super().get_queryset()
return queryset
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
The problem is that when you go to the next page, the form field is cleared and the query takes the value None query=None
. The entire pagination becomes undefined. How to save queryset when crossing pages in pagination?
答案1
得分: 1
这与视图的显示关系不大,而与分页链接有关。您可以在视图中创建一个辅助函数:
class SearchedResults(ListView):
# ... 其他部分 ...
def urlencode_search(self):
qd = self.request.GET.copy()
qd.pop(self.page_kwarg, None)
return qd.urlencode()
# ... 其他部分 ...
在模板中,您可以链接到不同的页面:
<a href="?page={{ page_obj.next_page_number }}&b&gt;&amp;amp;{{ view.urlencode_search }}&lt;/b&gt;&quot;&amp;gt;下一页&lt;/a&gt;
英文:
This has not much to do with the view, but the links of the pagination. You can create a helper function in the view:
<pre><code>class SearchedResults(ListView):
model = Quote
paginate_by = 10
template_name = 'quotes/index.html'
context_object_name = 'quotes'
def <b>urlencode_search</b>(self):
qd = self.request.GET.copy()
qd.pop(self.page_kwarg, None)
return qd.urlencode()
def get_queryset(self):
query = self.request.GET.get('search_query')
queryset = super().get_queryset(*args, **kwargs)
if query:
queryset = queryset.filter(
Q(quote__icontains=query)
| Q(tags__name__icontains=query)
| Q(author__fullname__icontains=query)
).distinct()
return queryset</code></pre>
In the template, you then link to a different page with:
<pre><code><a href="?page={{ page_obj.next_page_number }}<b>&amp;{{ view.urlencode_search }}</b>">next page</a></code></pre>
and this for all links that go to a page.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论