不确定如何看到Django-haystack的搜索结果?

时间:2016-02-24 22:56:20

标签: python django django-haystack

我最近设置了django和haystack,我没有错误,但如何查看我的搜索结果?它没有提到只为模板和网址设置视图。表单模板中的操作是

<form method="get" action=".">

没有用,所以我把它改成了

<form method="get" action="posts/search">

我也用搜索后的斜杠尝试了这个

<form method="get" action="posts/search/">

我也尝试给我的网址命名

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

并有这样的表格

<form method="GET" action="{% url 'posts:search' %}">

当我尝试它时,它给了我这个错误

django.core.urlresolvers.NoReverseMatch: Reverse for 'search' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

如何将我的应用正确链接到服务器,以便正确显示结果

继承我的代码 我的导航中的表格。动作是我在干草堆之前创建的页面,上面的那些是新的

<form method="GET" action="{% url 'posts:search-page' %}"  class="navbar-form navbar-right" role="search">
    <div>
        <div class="input-group">
            <input type="text" class="form-control" name="q" id="search" placeholder="search" value="{{ request.GET.q }}">
            <span class="input-group-btn">
                <button type="submit" class="btn btn-default">search</button>
            </span>
        </div><!-- /input-group -->
     </div><!-- /.col-lg-6 -->
</form>

我的帖子/ urls.py

from django.conf.urls import url, include
from .views import post_create, post_detail, post_list, post_update, post_delete, post_search, tag_list

urlpatterns = [
    url(r'^$', post_list, name='list'),
    url(r'^create/$', post_create, name='create'),
    url(r'^search_results/$', post_search, name='search-page'),
    url(r'^tag/(?P<slug>[\w-]+)/$', tag_list, name="tag_list"),
    url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
    url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
    url(r'^(?P<id>\d+)/delete/$', post_delete, name='delete'),
    url(r'^search/', include('haystack.urls')),
]

我的searche_indexes.py

from haystack import indexes
from .models import Post


class NoteIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title')
    publish = indexes.DateTimeField(model_attr='publish')

    # content_auto = indexes.EdgeNgramField(model_attr='title')

    def get_model(self):
        return Post

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.all()

我的模板/ search / search.html

{% extends 'base.html' %}

{% block content %}
<h2>Search</h2>

<form method="get" action=".">
    <table>
        {{ form.as_table }}
        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" value="Search">
            </td>
        </tr>
    </table>

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

my post_text.txt

{{ object.title }}
{{ object.body }}

我的帖子模型

class Post(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    title = models.CharField(max_length=120)
    slug = models.SlugField(max_length=200, unique=True)
    image = models.ImageField(upload_to=upload_location,
                              null=True,
                              blank=True,
                              width_field="width_field",
                              height_field="height_field")
    height_field = models.IntegerField(default=0)
    width_field = models.IntegerField(default=0)
    content = models.TextField()
    draft = models.BooleanField(default=False)
    publish = models.DateField(auto_now=False, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    tags = models.ManyToManyField(Tag)

    objects = PostManager()

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"slug": self.slug})

    class Meta:
        ordering = ["-timestamp", "-updated"]

正如我所说,我没有系统错误。这是我第四次尝试使用它。现在没有错误,只是一些与显示结果有关的问题。欢迎任何正确方向的指导

0 个答案:

没有答案