2016-11-04 78 views
1

我有一個基本的Django乾草堆SearchForm工作正常,但現在我試圖創建一個自定義搜索表單,其中包括一些額外的字段進行過濾。Django Haystack自定義搜索表單

我跟着上創建自定義表單和視圖草堆文件,但是當我嘗試查看的形式,我只能得到錯誤:

ValueError at /search/calibration/ 

The view assetregister.views.calibration_search didn't return an HttpResponse object. It returned None instead. 

不應SearchForm此基礎照顧返回一個HttpResponse對象?

forms.py

from django import forms 
from haystack.forms import SearchForm 

class CalibrationSearch(SearchForm): 
    calibration_due_before = forms.DateField(required=False) 
    calibration_due_after = forms.DateField(required=False) 

    def search(self): 
     #First we need to store SearchQuerySet recieved after/from any other processing that's going on 
     sqs = super(CalibrationSearch, self).search() 

     if not self.is_valid(): 
      return self.no_query_found() 

     #check to see if any date filters used, if so apply filter 
     if self.cleaned_data['calibration_due_before']: 
      sqs = sqs.filter(calibration_date_next__lte=self.cleaned_data['calibration_due_before']) 

     if self.cleaned_data['calibration_due_after']: 
      sqs = sqs.filter(calibration_date_next__gte=self.cleaned_data['calibration_due_after']) 

     return sqs 

views.py

from .forms import CalibrationSearch 
from haystack.generic_views import SearchView 
from haystack.query import SearchQuerySet 


def calibration_search(SearchView): 
    template_name = 'search/search.html' 
    form_class = CalibrationSearch 
    queryset = SearchQuerySet().filter(requires_calibration=True) 

    def get_queryset(self): 
     queryset = super(calibration_search, self).get_queryset() 
     return queryset 

urls.py

from django.conf.urls import include, url 
from . import views 

urlpatterns = [ 
    .... 
    url(r'^search/calibration/', views.calibration_search, name='calibration_search'), 
    .... 
] 

回答

2

草堆的SearchView是一種基於類視圖,你必須調用.as_view()類方法時添加一個URL條目。

url(r'^search/calibration/', views.calibration_search.as_view(), name='calibration_search'), 
+0

哎喲,這樣的小事情仍然趕上我! 修復,導致我另一個錯誤'對象沒有屬性'as_view'',這是因爲我用'def calibration_search(SearchView):'而不是'class calibration_search(SearchView):' 至少我可以現在正確地查看錶單,唯一的問題是無論我搜索什麼,它都不會返回任何結果... – TimJ

+0

您是否使用'rebuild_index'命令將數據添加到了搜索索引? – v1k45

+0

我有,我知道索引正在工作,因爲我可以使用乾草堆瀏覽器在管理界面中查看它,而我的其他(主)搜索表單仍然可以正常工作。 由於某種原因,我的自定義表單沒有得到任何結果。 – TimJ