Django-Haystack Faceting Initial Set up

时间:2016-05-05 00:05:11

标签: python django django-haystack

Haystack 2.4.1(Django 1.9)

我在分面搜索方面遇到了麻烦。

我尝试按照this最近的Stack Overflow回答中的示例,但它仍无效。

来自Searchapp

#search/views.py
from django.shortcuts import render
from haystack.forms import FacetedSearchForm
from haystack.generic_views import FacetedSearchView as BaseFacetedSearchView

class FacetedSearchView(BaseFacetedSearchView):
        form_class = FacetedSearchForm
        facet_fields = ['categories']
    template_name = 'search/search.html'
    context_object_name = 'page_object'

#urls.py
from haystack.forms import FacetedSearchForm
from search.views import FacetedSearchView
from haystack.query import SearchQuerySet

urlpatterns += patterns('haystack.views',
    url(r'^search/', FacetedSearchView.as_view(), name='haystack_search'),
)

我很感激你们能提供的任何帮助。在这个问题上,我的斗智斗勇。

我正在使用Haystack文档中的模板。

{% if query %}
    <!-- Begin faceting. -->
    <h2>By Category</h2>

    {{ self.get_facets }}

    <div>
        <dl>
            {% if facets.fields.category %}
                <dt>category</dt>
                {# Provide only the top 5 categories #}
                {% for category in facets.fields.categories|slice:":5" %}
                    <dd><a href="{{ request.get_full_path }}&amp;selected_facets=category_exact:{{ category.0|urlencode }}">{{ category.0 }}</a> ({{ category.1 }})</dd>
                {% endfor %}
            {% else %}
                <p>No category facets.</p>
            {% endif %}
        </dl>
    </div>
    <!-- End faceting -->

    <!-- Display results... -->
    {% for result in page_object %}
        <div class="search_result">
            <h3><a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a></h3>

            <p>{{ result.object.body|truncatewords:80 }}</p>
        </div>
    {% empty %}
        <p>Sorry, no results found.</p>
    {% endfor %}

{% endif %}

0 个答案:

没有答案