Django:使用基于类的视图进行搜索结果的分页

huangapple go评论107阅读模式
英文:

Dajngo: Pagination in search results with Class-based views

问题

我想要根据我在表单中输入的关键词进行分页。

我使用以下类

  1. def pageNotFound(request, exceprion):
  2. return HttpResponseNotFound("<h2>Page not found</h2>")
  3. def get_quotes():
  4. top_tags = get_top_tags()
  5. context = {
  6. "top_tags": top_tags,
  7. "functional_menu": functional_menu,
  8. }
  9. return context
  10. class Main(ListView):
  11. model = Quote
  12. paginate_by = 10
  13. template_name = "quotes/index.html"
  14. context_object_name = "quotes"
  15. def get_context_data(self, **kwargs):
  16. context = super().get_context_data(**kwargs)
  17. context.update(get_quotes())
  18. return context
  19. class SearchedResults(ListView):
  20. model = Quote
  21. paginate_by = 10
  22. template_name = "quotes/index.html"
  23. context_object_name = "quotes"
  24. def get_queryset(self):
  25. query = self.request.GET.get("search_query")
  26. if query:
  27. queryset = Quote.objects.filter(
  28. Q(quote__icontains=query)
  29. | Q(tags__name__icontains=query)
  30. | Q(author__fullname__icontains=query)
  31. ).distinct()
  32. else:
  33. queryset = super().get_queryset()
  34. return queryset
  35. def get_context_data(self, **kwargs):
  36. context = super().get_context_data(**kwargs)
  37. return context

问题是,当您跳转到下一页时,表单字段被清空,查询变成了 None query=None。整个分页变得未定义。如何在分页时保存查询集(queryset)?

英文:

I want to paginate by keywords that I enter in the form.

I use the following class

  1. def pageNotFound(request, exceprion):
  2. return HttpResponseNotFound(&quot;&lt;h2&gt;Page not found&lt;/h2&gt;&quot;)
  3. def get_quotes():
  4. top_tags = get_top_tags()
  5. context = {
  6. &quot;top_tags&quot;: top_tags,
  7. &quot;functional_menu&quot;: functional_menu,
  8. }
  9. return context
  10. class Main(ListView):
  11. model = Quote
  12. paginate_by = 10
  13. template_name = &quot;quotes/index.html&quot;
  14. context_object_name = &quot;quotes&quot;
  15. def get_context_data(self, **kwargs):
  16. context = super().get_context_data(**kwargs)
  17. context.update(get_quotes())
  18. return context
  19. class SearchedResults(ListView):
  20. model = Quote
  21. paginate_by = 10
  22. template_name = &quot;quotes/index.html&quot;
  23. context_object_name = &quot;quotes&quot;
  24. def get_queryset(self):
  25. query = self.request.GET.get(&quot;search_query&quot;)
  26. if query:
  27. queryset = Quote.objects.filter(
  28. Q(quote__icontains=query)
  29. | Q(tags__name__icontains=query)
  30. | Q(author__fullname__icontains=query)
  31. ).distinct()
  32. else:
  33. queryset = super().get_queryset()
  34. return queryset
  35. def get_context_data(self, **kwargs):
  36. context = super().get_context_data(**kwargs)
  37. 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

这与视图的显示关系不大,而与分页链接有关。您可以在视图中创建一个辅助函数:

  1. class SearchedResults(ListView):
  2. # ... 其他部分 ...
  3. def urlencode_search(self):
  4. qd = self.request.GET.copy()
  5. qd.pop(self.page_kwarg, None)
  6. return qd.urlencode()
  7. # ... 其他部分 ...

在模板中,您可以链接到不同的页面:

  1. <a href="?page={{ page_obj.next_page_number }}&amp;b&amp;gt;&amp;amp;amp;{{ view.urlencode_search }}&amp;lt;/b&amp;gt;&amp;quot;&amp;amp;gt;下一页&amp;lt;/a&amp;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'

  1. def &lt;b&gt;urlencode_search&lt;/b&gt;(self):
  2. qd = self.request.GET.copy()
  3. qd.pop(self.page_kwarg, None)
  4. return qd.urlencode()
  5. def get_queryset(self):
  6. query = self.request.GET.get(&#39;search_query&#39;)
  7. queryset = super().get_queryset(*args, **kwargs)
  8. if query:
  9. queryset = queryset.filter(
  10. Q(quote__icontains=query)
  11. | Q(tags__name__icontains=query)
  12. | Q(author__fullname__icontains=query)
  13. ).distinct()
  14. return queryset&lt;/code&gt;&lt;/pre&gt;

In the template, you then link to a different page with:

<pre><code>&lt;a href=&quot;?page={{ page_obj.next_page_number }}<b>&amp;amp;{{ view.urlencode_search }}</b>&quot;&gt;next page&lt;/a&gt;</code></pre>

and this for all links that go to a page.

huangapple
  • 本文由 发表于 2023年6月26日 00:15:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定