简体   繁体   English

如何在SAME页面上显示搜索结果(django + haystack + elasticsearch)?

[英]How to display search results(django+haystack+elasticsearch) on the SAME page?

I included haystack+elasticsearch into my django project folowing by official haystack docs. 我将haystack + elasticsearch纳入了官方haystack文档所遵循的django项目中。 Searching is working good, but results are displaying in separate page (search.html).I need show searching results on the same page I place searching query from. 搜索工作正常,但结果显示在单独的页面(search.html)中。我需要在放置搜索查询的同一页面上显示搜索结果。 I include search template into my base.html like this: {% include 'search/search.html' %} My templates are in different dirs:templates/students/base.html and templates/search/search.html. 我将搜索模板包括到我的base.html中,如下所示:{%include'search / search.html'%}我的模板位于不同的目录中:templates / students / base.html和templates / search / search.html。 As far I understand haystack uses its own /search/search.html for displaying search results. 据我了解,haystack使用其自己的/search/search.html来显示搜索结果。 Which way can I change that behavior, how to display results on the same page? 我可以通过哪种方式更改该行为,如何在同一页面上显示结果? Please help! 请帮忙!

urls.py: urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin

from students.views.students import StudentUpdateView
from students.views.students import StudentDeleteView
from students.views.students import StudentAddView
from students.views.groups import GroupDeleteView
from students.views.journal import JournalView

urlpatterns = patterns('',

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

# main page url

    url(r'^$', 'students.views.students.students_list', name ='home'),

search.html: search.html:

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

        </td>
    </tr>
</table>
    {% for student in page.object_list %}
    <p><span style= "color:blue">Student:</
    span> {{ student.object.first_name }}&nbsp;{{student.object.last_name }}</p>

   <p>Ticket: {{ student.object.ticket }}</p>
   <p>Group: {{ student.object.student_group }}</p>

  {% empty %}
    <p>No results found.</p>
  {% endfor %}

seach_indexes.py: seach_indexes.py:

from haystack import indexes
from students.models.students import Student

class StudentIndex(indexes.SearchIndex, indexes.Indexable):
    text             = indexes.CharField(document=True, use_template=True) 
    last_name     = indexes.CharField(model_attr='last_name')

    def get_model(self):
        return Student

    def index_queryset(self, using=None):       
        return self.get_model().objects.all()

You can create a custom view and use that in your templates: 您可以创建一个自定义视图,并在模板中使用它:

class CustomSearchView(SearchView):
    template_name='/path/to/template.html'

and in your urls.py : 并在您的urls.py

urlpatterns = patterns('',
    url(r'^custom_search$', CustomSearchView.as_view(), name='custom_search'),
)

and in your templates, just call this view in your form: 在您的模板中,只需在表单中调用此视图即可:

<form method="get" action="{% url 'search:custom_search' %}">

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

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