简体   繁体   English

Django-Haystack在搜索表单中未返回任何结果

[英]Django-Haystack returns no results in search form

I am using Django-Haystack with Whoosh backend. 我正在使用带有Whoosh后端的Django-Haystack。 When I do a query I get no results. 当我查询时,没有任何结果。 I tried the debugging steps suggested in the Haystack docs by typing the following into a Django shell, and I can see that all the text I want has been indexed. 通过在Django shell中键入以下内容,我尝试了Haystack文档中建议的调试步骤,并且可以看到我想要的所有文本都已被索引。

from haystack.query import SearchQuerySet
sqs = SearchQuerySet().all()
sqs.count()
sqs[0].text

My search.html page has the following section (copied straight from the documentation): 我的search.html页面包含以下部分(直接从文档中复制):

{% for result in page.object_list %}
    <p>
        <a href="{{ result.object.url }}">{{ result.object }}</a>
    </p>
{% empty %}
    <p>No results found.</p>
{% endfor %}

What else can I try? 我还能尝试什么?

As a noobie trying out django-haystack and whoosh, and following fragments of various tutorials on haystack's docs, I had the same problem as you: No results were showing up when I did an EmptySearch(), even though I had overridden SearchForm to show all. 作为一个尝试django-haystack和whoosh的新手,并遵循haystack文档中各种教程的片段,我遇到了与您相同的问题:当我执行EmptySearch()时,即使我覆盖了SearchForm来显示,也没有显示任何结果所有。

def no_query_found(self):
    return self.searchqueryset.all()

As you say, removing the "page" prefix on the search.html template did the trick, and was a good temporary solution. 如您所说,删除search.html模板上的“ page”前缀可以解决问题,这是一个很好的临时解决方案。 However, it became a problem when it was time to paginate the results. 但是,当需要对结果进行分页时,这成为一个问题。 So after looking around, the solution was to use the "page_obj" prefix instead of "page" and everything works as expected. 因此,环顾四周后,解决方案是使用“ page_obj”前缀而不是“ page”,并且一切正常。 It seems the issue is that the haystack-tutorial assumes the page object is called "page", while certain versions of django its called "page_obj"? 看来问题在于haystack-tutorial假定页面对象称为“页面”,而某些版本的django称为“ page_obj”吗? I'm sure there is a better answer - I'm just reporting my limited findings. 我敢肯定会有更好的答案-我只是在报告我的有限发现。

Well, I have no idea what's happening, but whereas in the examples page.object_list works, in my real project I needed to remove the page prefix. 好吧,我不知道发生了什么,但是在示例page.object_list可以工作的情况下,在我的真实项目中,我需要删除page前缀。 Painful to figure out. 痛苦地想出办法。

Now this works: 现在工作:

{% for result in object_list %}
  <p>
    <a href="{{ result.object.url }}">{{ result.object }}</a>
  </p>
{% empty %}
  <p>No results found.</p>
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM