在模板中使用django haystack搜索与全局搜索栏 [英] Using django haystack search with global search bar in template

查看:158
本文介绍了在模板中使用django haystack搜索与全局搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django项目,需要搜索2种不同的型号,其中一种型号有3种类型,我需要基于过滤器。我已经安装了干草堆并在基本意义上工作(对于我的模型使用默认的url conf和SearchView,并且从入门文档中的模板返回结果很好)。



问题是,我只能通过在基本的search.html模板中使用搜索表单来获取结果,而我正在尝试使用全局搜索栏来工作,但是我似乎无法正确地使用它干草堆文件没有太多的运气。我在这里找到另一个问题,导致我在搜索应用程序中使用以下方法。



我的urls.py将/搜索添加到我的search.views中的此视图:

  def search_posts(request):
post_type = str(request.GET.get('type')) ()
view_class = SearchView,
template ='search / search .html',
searchqueryset = sqs,
form_class = HighlightedSearchForm

返回视图(请求)

输入的url字符串看起来像:



http://example.com/search/?q=test&type=blog



这将从我的全局搜索栏获取查询字符串,但不返回任何结果,但是如果我从sqs行中删除.filter(type = post_type)部分,我将获得拱结果再次(尽管不是通过帖子类型过滤)。有任何想法吗?我想我缺少一些相当明显的东西,但我似乎无法弄清楚这一点。



谢谢
-Sean



编辑:



原来我只是个白痴。为什么我通过类型过滤SQS的原因是没有返回结果是因为我的PostIndex类中没有包含类型字段。我将PostIndex更改为:

  class PostIndex(indexes.SearchIndex,indexes.Indexable):
...
type = indexes.CharField(model_attr ='type')

并重新构建,



感谢您的回复!

解决方案

  def search_posts(request):
post_type = str(request.GET.get('type'))。lower()
sqs = SearchQuerySet() post_type)
clean_query = sqs.query.clean(post_type)
result = sqs.filter(content = clean_query)
view = search_view_factory(
view_class = SearchView,
template ='search / search.html',
searchqueryset = result,
form_class = HighlightedSearchForm

返回视图(请求)


I have a django project that needs to search 2 different models and one of the models has 3 types that I need to filter based on. I have haystack installed and working in a basic sense (using the default url conf and SearchView for my model and the template from the getting started documentation is returning results fine).

The problem is that I'm only able to get results by using the search form in the basic search.html template and I'm trying to make a global search bar work with haystack but I can't seem to get it right and I'm not having a lot of luck with the haystack documentation. I found another question on here that led me to the following method in my search app.

my urls.py directs "/search" to this view in my search.views:

def search_posts(request):
    post_type = str(request.GET.get('type')).lower()
    sqs = SearchQuerySet().all().filter(type=post_type)
    view = search_view_factory(
        view_class=SearchView,
        template='search/search.html',
        searchqueryset=sqs,
        form_class=HighlightedSearchForm
        )
    return view(request)

The url string that comes in looks something like:

http://example.com/search/?q=test&type=blog

This will get the query string from my global search bar but returns no results, however if I remove the .filter(type=post_type) part from the sqs line I will get search results again (albeit not filtered by post type). Any ideas? I think I'm missing something fairly obvious but I can't seem to figure this out.

Thanks, -Sean

EDIT:

It turns out that I am just an idiot. The reason why my filtering on the SQS by type was returning no results was because I didn't have the type field included in my PostIndex class. I changed my PostIndex to:

class PostIndex(indexes.SearchIndex, indexes.Indexable):
      ...
      type = indexes.CharField(model_attr='type')

and rebuilt and it all works now.

Thanks for the response though!

解决方案

def search_posts(request):
    post_type = str(request.GET.get('type')).lower()
    sqs = SearchQuerySet().filter(type=post_type)
    clean_query = sqs.query.clean(post_type)
    result = sqs.filter(content=clean_query)
    view = search_view_factory(
        view_class=SearchView,
        template='search/search.html',
        searchqueryset=result,
        form_class=HighlightedSearchForm
        )
    return view(request)

这篇关于在模板中使用django haystack搜索与全局搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆