saurav
saurav

Reputation: 141

changing the default view in django-haystack

I am new to django & haystack. I followed the example tutorial provided in haystack website and figured out how to perform a basic search.I am able get the output values but the page is not displaying correctly. Haystack default form is searchForm but in my case default is ModelSearchForm. I don't know why and how to change it.

This is the form in my search.html page.

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

In my search page ,haystack always shows me a default form. It has predefined labels,it always shows me the model name with a checkbox.I am not able to figure out from where it is displaying these values and how to modify that.

My search_index.py file

import datetime
from haystack import indexes
from signups.models import SignUp


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

    def get_model(self):
        return SignUp

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

My model.py file

from django.db import models
from django.utils.encoding import smart_unicode


class SignUp(models.Model):
    first_name = models.CharField(max_length = 120,null = True, blank= True)
    last_name = models.CharField(max_length = 120,null = True, blank= True)
    email = models.EmailField()
    timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
    update = models.DateTimeField(auto_now_add = False, auto_now = True)

    def __unicode__(self):
        return smart_unicode(self.email)

I wished to add a image to communicate more effectively but not able to because of less reputation. :(

Upvotes: 2

Views: 670

Answers (1)

bre
bre

Reputation: 890

You can change the form class and the view class in urls.py. Something like:

from haystack.views import SearchView, search_view_factory
from haystack.forms import HighlightedModelSearchForm

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mamotwo.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^search/', include('haystack.urls')),
    url(r'^mysearch/$', search_view_factory(view_class=SearchView, form_class=HighlightedModelSearchForm), name='mysearch'),
    url(r'^admin/', include(admin.site.urls)),
)

But i'm new to haystack too. Better if you check this code haystack demo or this video haystack demo video

Upvotes: 1

Related Questions