2014-07-02 58 views
2

我是新來的django &乾草堆。我遵循haystack網站提供的示例教程,並想出如何執行基本搜索。我能夠獲取輸出值,但頁面顯示不正確。 Haystack默認格式爲searchForm,但在我的情況下,默認格式爲ModelSearchForm。我不知道爲什麼以及如何改變它。更改django-haystack的默認視圖

這是我的search.html頁面中的表單。

<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> 

在我的搜索頁面,乾草堆總是顯示我一個默認窗體。它有預定義的標籤,它總是用複選框向我顯示模型名稱。我無法弄清楚它顯示這些值的位置以及如何修改它。

search_index.py文件

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() 

model.py文件

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) 

我想補充一個形象,更有效,但無法溝通,因爲較少的聲譽。 :(

回答

1

您可以更改窗體類和urls.py. 有點像視圖類:

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)), 
) 

但我是新來的草垛也更好,如果你檢查這個代碼haystack demo或本視頻haystack demo video