Haystack自定义搜索表单

Haystack自定义搜索表单

本文介绍了Django Haystack自定义搜索表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的django-haystack SearchForm可以正常工作,但现在我正在尝试创建一个自定义搜索表单,其中包含几个要过滤的额外字段。



我遵循了关于创建自定义表单和视图的Haystack文档,但是当我尝试查看表单时,我只能得到错误:

  / search / calibration / 

的ValueError视图assetregister.views.calibration_search没有返回HttpResponse对象。它返回无。

不应该在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):
#首先我们需要存储SearchQuerySet在/从任何其他处理之后收到
sqs = super(CalibrationSearch,self).search()

如果不是self.is_valid():
return self.no_query_found()

#check查看是否使用任何日期过滤器,如果是这样应用过滤器
如果self.cleaned_data [ 'calibration_due_before']:
sqs = sqs.filter(calibration_date_next__lte = self.cleaned_data ['calibration_due_before'])

如果self.cleaned _data ['calibration_due_after']:
sqs = sqs.filter(calibration_date_next__gte = self.cleaned_data ['calibration_due_after'])

返回sqs
/ pre>

views.py

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


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

def get_queryset(self):
queryset = super(calibr_search,self) get_queryset()
return queryset

urls.py

  from django.conf.urls import include,url 
from。导入视图

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


解决方案

干草堆 SearchView 是一个基于类的视图,您必须在添加urls条目时调用 .as_view()类方法。 / p>

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


I've got a basic django-haystack SearchForm working OK, but now I'm trying to create a custom search form that includes a couple of extra fields to filter on.

I've followed the Haystack documentation on creating custom forms and views, but when I try to view the form I can only get the error:

ValueError at /search/calibration/

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

Shouldn't basing this on SearchForm take care of returning a HttpResponse object?

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'),
    ....
]
解决方案

Haystack's SearchView is a class based view, you have to call .as_view() class method when adding a urls entry.

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

这篇关于Django Haystack自定义搜索表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:52