Django-Haystack returns no results in search form

自闭症网瘾萝莉.ら 提交于 2019-12-08 06:19:53

问题


I am using Django-Haystack with Whoosh backend. 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.

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):

{% 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?


回答1:


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.

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. 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. 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"? I'm sure there is a better answer - I'm just reporting my limited findings.




回答2:


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. 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 %}


来源:https://stackoverflow.com/questions/36660747/django-haystack-returns-no-results-in-search-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!