首页 文章

干草堆自定义表单字段未显示在模板中

提问于
浏览
1

我想在 Haystack 表单中添加一个额外的字段,但由于某种原因,模板中的表单输出不起作用 .

我没有显示额外的字段,而是使用模型选择字段获得了一些奇怪的模板 . 我想要的只是一个 q 输入和 author 的额外字段 .

forms.py

from django import forms
from haystack.forms import HighlightedSearchForm

class QuestionSearchForm(HighlightedSearchForm):
    author = forms.CharField(max_length = 100, required = False)

views.py

# Create your views here.
from haystack.generic_views import SearchView
from search.forms import QuestionSearchForm


class QuestionSearchView(SearchView):
    form_class = QuestionSearchForm
    template_name = 'search/search.html'

search/search.html

<form method="get" class="form-inline" action=".">
    <div>
        {{ form.as_p }}
        <input class="btn btn-primary" type="submit" value="Najdi"/>
    </div>
</form>

Output:

enter image description here

谢谢!

1 回答

  • 1

    我相信 template_name 应该是你的views.py中的 template .

    看一下haystack Github page.上的默认 class SearchView

相关问题