0

I have implemented a working Django 1.6 haystack/whoosh search field following Mike Hibbert's tutorial:https://www.youtube.com/watch?v=B-n6_m66TmA. The implementation works in a twitter bootstrap search field in the navbar in the url 'search'. I'd like to use the implementation in several pages in my application but it doesn't work.

I've tried with implementing the search.html code in other pages with search bars but it changes the search url prefix from 'search/...' to the page url, e.g. to 'front/...' on the frontpage. I've also tried to include the search.html in other pages, both as the block content and with an include tag in the template but it hasn't worked.

# in app/templates/search/indexes/search.html

{% extends 'base_searches.html' %}
{% block content %}

<form method="get" action=".">
    <table>
<tr><th><label for="id_q"></label></th><td><input type="text" id="id_q"
  name="q" placeholder="Search..." class="form-control" /><button 
 class="btn btn-success" type="submit" value="Search"><spaclass="glyphicon
  glyphicon-search"></span></button></td></tr>
        <tr>
            <td>&nbsp;</td>

        {% if query %}

        {% for result in page.object_list %}

        <div class="alert alert-success" role="alert">
          Hi, {{ user }}, here's the found database match for your search 
         <a href="{{ result.object.get_absolute_url }}">
         {{result.object.title }}.</a></div>


        {% empty %}

               <div class="alert alert-info" role="alert">
              <strong>Hi {{ user }}, there's no match found in the 
            database for your search !.</strong>

              </div>
        {% endfor %}

        {% endif %}
        <div class="col-xs-6 col-sm-3 sidebar-offcanvas" id="sidebar">

                 <!--/.sidebar-offcanvas-->
           <!--/row-->

              <hr>

            <td>

            </td>
        </tr>
       </table>
     </form>

    </body>
   </html>

  {% endblock %}

# in models.py for each indexed model field
def get_absolute_url(self):
    return '%s' % self.id


# in app/templates/base_searches.html

<form class="navbar-form navbar-right" id="search">{% csrf_token %}

<center><h> <td colspan="1" id="content">{% block content %}}
 {% endblock %} </center></td></h>

 <ul id="search-results">
    </ul>

  </form>

 in urls.py
 from haystack.query import SearchQuerySet

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

 # in search_indexes.py

 from haystack import indexes
 from app.models import Article
 class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
       text= indexes.CharField(document=True,use_template=True)
       content = indexes.CharField(model_attr='description')
       content_auto = indexes.EdgeNgramField(model_attr='title')
       def get_model(self):
          return Article
       def index_queryset(self, using=None):
          """used when the entire index for model is updated """

           return self.get_model().objects.all()



# in app/templates/search/indexes/appname/article_text.txt
{{ object.title }}
{{ object.content}}

How do I include the search.html in other pages, as I've included it in the navbar in base_searches.html, and maintain the prefix search/..searchresult for the get_absolut_url function for the search result object and not the url prefix for other pages that I'm trying to include it in, e.g. front/..searchresult ? when trying to implement it in the frontpage with url front/, or are there better ways to use the haystack whoosh search in search fields in multiple application pages ?

Kewin Rozz
  • 41
  • 6

1 Answers1

0

I have solved this problem by adding http://{{ request.get_host }}/search" to the search form action.

`<form method="get" action="http://{{ request.get_host }}/search"`

The action="." will reference to your current page and the request will be redirected to your current view. With adding the host URL to your action it will redirect your query to the search view no matter on which page you are.

silversky
  • 1
  • 1