gpt4 book ai didi

python - Django Haystack 自定义搜索表单

转载 作者:行者123 更新时间:2023-12-01 03:35:38 27 4
gpt4 key购买 nike

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

我已按照 Haystack 文档创建自定义表单和 View ,但是当我尝试查看表单时,我只能收到错误:

ValueError at /search/calibration/

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

基于 SearchForm 不应该负责返回 HttpResponse 对象吗?

表单.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

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

url.py

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

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

最佳答案

Haystack 的 SearchView 是基于类的 View ,添加 url 条目时必须调用 .as_view() 类方法。

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

关于python - Django Haystack 自定义搜索表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40423811/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com