问题
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