使用两个不同的模板搜索结果 - Haystack

时间:2015-05-29 18:07:05

标签: templates search django-haystack

我完全不知道如何为搜索结果使用两个不同的模板文件。假设我有两个不同的搜索页面,我希望每个页面搜索结果都有不同的装饰。

此搜索栏位于以下页面中:

<div class="main-header_search">
                <form method="get" action="/search1" id="header_find_form" class="main-search yform" role="search"
                      data-component-bound="true">
                    <div class="arrange arrange--middle arrange--6">
                        <div class="arrange_unit" style="width: 100%;">
                            <div class="main-search_suggestions-field search-field-container find-decorator">
                                <label class="main-search_pseudo-input pseudo-input">
                                    <span class="pseudo-input_text">Find</span>
                                    <span class="pseudo-input_field-holder">
                                        <input autocomplete="off" type="search" id="id_q" maxlength="64" name="q"
                                               placeholder="Cheap dinner, Cafe" value=""
                                               class="main-search_field pseudo-input_field"
                                               aria-autocomplete="list" tabindex="1" data-component-bound="true">
                                    </span>
                                </label>

                                <div class="main-search_suggestions suggestions-list-container search-suggestions-list-container hidden"
                                     data-component-bound="true">
                                    <ul class="suggestions-list" role="listbox" aria-label="Search results"></ul>
                                </div>
                            </div>
                        </div>
                        <div class="arrange_unit main-search_actions">

                            <button class="ybtn ybtn-primary main-search_submit" id="header-search-submit"
                                    tabindex="3"
                                    title="Search" type="submit" value="search">
                                <i class="i ig-common_sprite i-search-common_sprite"></i>


                            </button>
                        </div>
                    </div>
                </form>

            </div>

这是在另一页:

<form method="get" name="business_search_form" class="yform business-search-form"
                  action="/search2" role="search">
                <div class="arrange arrange--6 arrange--stack">
                    <div class="arrange_unit arrange_unit--fill">
                        <div class="arrange arrange--equal arrange--6 arrange--stack">
                            <div class="arrange_unit">
                                <label class="responsive-visible-small-block hidden-non-responsive-block">Business
                                    Name</label>

                                <div class="pseudo-input business-find-decorator">
                                    <div class="flex-container-inline">
                                        <div class="label responsive-hidden-small">Business Name</div>
                                        <div class="flex-box input-holder">
                                            <input class="landing-search-biz-name" placeholder="e.g. Mel's Diner"
                                                   name="q" autocomplete="off" type="text">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="arrange_unit nowrap">
                        <button type="search" value="search"
                                class="ybtn ybtn-primary yform-search-button ybtn-full-responsive-small"><span><span
                                class="i-wrap ig-wrap-common i-search-dark-common-wrap"><i
                                class="i ig-common i-search-dark-common"></i> Get Started</span></span></button>
                    </div>
                </div>
            </form>

我想在urls.py中使用这样的内容,为每个搜索结果提供两个不同的模板:

(r'^search1/', include('haystack.urls'), name='template1'),
(r'^search2/', include('haystack.urls'),name='template2'),

我在我的setting.py中使用简单的haystack后端:

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
    },
}

1 个答案:

答案 0 :(得分:1)

我将如何做到这一点:

首先,创建一个继承haystack SearchForm的搜索表单:

class ItemSearchForm(SearchForm):
    def search(self, request):
        items = super(ItemSearchForm, self).search().models(Item)  // Item is your model name
        shops = super(ItemSearchForm, self).search().models(Shop)
        return {'items': items, 'shops': shops}

第二次,创建使用此表单的视图:

def searchItems(request):
    form = ItemSearchForm(request.GET)
    results = form.search(request)
    return render(request, 'search/search.html', {
        'items': results['items']
    }) 

def searchShops(request):
    form = ItemSearchForm(request.GET)
    results = form.search(request)
    return render(request, 'search/search.html', {
        'shops': results['shops']
    })

请注意,这两个视图使用不同的return值。您可以更改它以修复您的情况。

然后,配置网址:

(r'^search1/', 'views.search1', name='search1'),
(r'^search2/', 'views.search2', name='search2'),

唯一要做的就是编写indexes文件和文档。这应该做到。