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

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

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(&quot;&lt;h2&gt;Page not found&lt;/h2&gt;&quot;)


def get_quotes():

    top_tags = get_top_tags()

    context = {
        &quot;top_tags&quot;: top_tags,
        &quot;functional_menu&quot;: functional_menu,
    }

    return context


class Main(ListView):
    model = Quote
    paginate_by = 10
    template_name = &quot;quotes/index.html&quot;
    context_object_name = &quot;quotes&quot;

    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 = &quot;quotes/index.html&quot;
    context_object_name = &quot;quotes&quot;

    def get_queryset(self):
        query = self.request.GET.get(&quot;search_query&quot;)
        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 }}&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'

def &lt;b&gt;urlencode_search&lt;/b&gt;(self):
    qd = self.request.GET.copy()
    qd.pop(self.page_kwarg, None)
    return qd.urlencode()

def get_queryset(self):
    query = self.request.GET.get(&#39;search_query&#39;)
    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&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:

确定