Reputation: 1654
I'm starting using Django Haystack with Elasticsearch.
All right until I started make a Custom View following the simple example in readthedocs.
search_indexes.py:
class ExperimentIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
owner = indexes.CharField(model_attr='owner')
def get_model(self):
return Experiment
def index_queryset(self, using=None):
return self.get_model().lastversion_objects.all()
urls.py:
url(r'^search/?$', NepSearchView.as_view(), name='search_view')
urls.py before (without Custom View):
url(r'^search/', include('haystack.urls'))
views.py
class NepSearchView(SearchView):
def get_queryset(self):
queryset = super(NepSearchView, self).get_queryset()
if not self.request.user.is_authenticated and \
self.request.user.groups.filter(name='trustees').exists():
return queryset # (with some filter)
else:
return queryset
search.html:
{# ... #}
{% for result in page.object_list %}
{% if result.model_name == 'experiment' %}
{% include 'search/experiments.html' %}
{% endif %}
{% if result.model_name == 'study' %}
{% include 'search/studies.html' %}
{% endif %}
{% if result.model_name == 'group' %}
{% include 'search/groups.html' %}
{% endif %}
{% if result.model_name == 'experimentalprotocol' %}
{% include 'search/experimental_protocol.html' %}
{% endif %}
{# ... #}
Well, the fact is when using default Haystack SearchView
I've got the correct matches, while when introducing NepSearchView
, page.object_list
is empty and I get No results found.
in template.
I already ran manage.py rebuild_index
, searched extensively in web but couldn't find nothing that explains what I'm missing.
Upvotes: 1
Views: 652
Reputation: 74
It seems that the page.object_list variable name for the queryset does not exist. Try the object_list without the 'page' prefix.
instead of
{% for result in page.object_list %}
use
{% for result in object_list %}
Alternatively you can provide a custom variable name in the view by appending something like this
context_object_name = 'haystack_objects'
and use it in the template
{% for result in haystack_objects %}
Upvotes: 2