Django Haystack在索引模板内搜索不返回任何内容

时间:2014-11-22 14:59:15

标签: django django-haystack

所以我已经关注了干草堆文档,但我无法跳转到我的应用程序中。

按照说明我在搜索/返回结果时有模板/ search / search.html,但我希望我的索引页面(我的首页)返回搜索结果。

从索引中搜索并不会返回任何内容。我是Django的新手,需要一些帮助,我确定我需要创建一个视图来做到这一点,但我不知道如何做。

我的索引views.py:

def index(request):
     return render_to_response('index.html', {'categories': Category.objects.all(), 'entries': Entry.objects.all()[:5]})

我的index.html模板:

{% extends 'base.html' %}

{% load markdown_deux_tags %}

{% block title %}The NRSLTD Knowledge Base{% endblock %}

{% block content %}

<div class="search">
    <h2>Search</h2>
    <form type="get" action=".">
        <input type="text" name="q" value="Search here" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">
        <button class="btn btn-custom" type="submit">Search</button>
        {% if query %}
        <h3>Results</h3>

        {% for result in page.object_list %}
            <p>
                <a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
            </p>
        {% empty %}
            <p>No results found.</p>
        {% endfor %}

        {% if page.has_previous or page.has_next %}
            <div>
                {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
                |
                {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
            </div>
        {% endif %}
    {% else %}
        {# Show some example queries to run, maybe query syntax, something else? #}
    {% endif %}
    </form>

    {% for note in notes %}
    <h1>{{ note.title }}</h1>
    <p>
        {{ note.body }}
    </p>
    {% endfor %}
</div>

<div class="wrap">
    <h2 id="title">View the last five entries made and the most recent categories added.</h2>
    <div class="cat">
        <h2>Categories</h2>
        {% if categories %}
            <ul>
            {% for category in categories %}
                <li><a href="{{ category.get_absolute_url }}">{{ category.title }}</a></li>
            {% endfor %}
            </ul>
        {% else %}
            <p>There are no entries.</p>
        {% endif %}
    </div>

    <div class="ent">
        <h2>Entries</h2>
        {% if entries %}
            <ul>
            {% for entry in entries %}
                <li><a href="{{ entry.get_absolute_url }}">{{ entry.title }}</a></li>
                {{ entry.text|markdown }}
                {{ entry.tags }}
            {% endfor %}
            </ul>
        {% else %}
            <p>There are no entries.</p>
        {% endif %}
    </div>
</div>

{% endblock %}

Urls.py:

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

我已将search / search.html复制到index.html中,它看起来一样,我只需要它就像search / search.html一样返回结果。

更新:

添加了settings.py

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'knowledgebase',
'markdown_deux',
'haystack',
'bootstrap3',
)

HAYSTACK_CONNECTIONS = {
'default': {
    'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
    'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
},
}

# This line enables signal processor that will update_index for every change in models
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

0 个答案:

没有答案