简体   繁体   English

自定义视图不显示带有Elastic Search的Django Haystack中的结果

[英]Custom view does not show results in Django Haystack with Elastic Search

I'm starting using Django Haystack with Elasticsearch. 我开始在Elasticsearch中使用Django Haystack。

All right until I started make a Custom View following the simple example in readthedocs . 好的,直到我开始按照readthedocs中的简单示例制作“自定义视图”。

search_indexes.py: 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: urls.py:

url(r'^search/?$', NepSearchView.as_view(), name='search_view')

urls.py before (without Custom View): 之前的urls.py(无自定义视图):

url(r'^search/', include('haystack.urls'))

views.py 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: 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. 好吧,事实是,当使用默认的Haystack SearchView我具有正确的匹配项,而在引入NepSearchViewpage.object_list为空,而我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. 我已经运行了manage.py rebuild_index ,在网络中进行了广泛搜索,但找不到任何能解释我所缺少的内容。

It seems that the page.object_list variable name for the queryset does not exist. 似乎查询集的page.object_list变量名称不存在。 Try the object_list without the 'page' prefix. 尝试使用没有'page'前缀的object_list

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

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

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