无法加载Haystack自定义表单

时间:2014-01-04 07:57:03

标签: django-haystack search-form

我想构建一个自定义搜索表单。以下是我的代码。我的应用程序称为“查看器”。我一直在/viewer/search/...name'没有定义'CustomSearchForm'的“NameError”。请帮忙。我知道这是一个简单的错误。

来自viewer / urls.py:

from django.conf.urls import *

from viewer import views, forms
from haystack.views import SearchView

urlpatterns = patterns('',
    #viewer urls
    ...
    url(r'^search/$', SearchView(form_class=CustomSearchForm), name='haystack_search')
)

来自viewer / forms.py:

from django import forms
from haystack.forms import ModelSearchForm
from haystack.query import SearchQuerySet

class CustomSearchForm(ModelSearchForm):
    ...

2 个答案:

答案 0 :(得分:1)

以下是我找到的解决方案,采用了不同的方法:

urls.py:

url(r'^search/', 'viewer.views.search'),

views.py:

def search(request):
    from .forms import CustomSearchForm
    form = CustomSearchForm(request.GET)
    searchresults = form.search()
    return render(request, 'viewer/search.html', {'form' : form})

在viewer / search.html中:

{% extends 'base.html' %}

{% block content %}
<form type="get" action=".">
    {{form}}
    <button type="submit">Search</button>
</form>
{% endblock %} 

答案 1 :(得分:0)

遇到同样的问题,但在/viewer/search/...name&#39; SearchForm&#39;没有定义&#34;试图按照这里的指南: http://django-haystack.readthedocs.io/en/v2.6.0/views_and_forms.html

刚刚升级到Django 1.11.0对我来说很有用。也许它对你也有用。