Django haystack 自动完成
我正在尝试使用 django-haystack (现在已经大约 2 天了),并且我已经让基本的入门示例正常工作,它看起来非常令人印象深刻。我现在想尝试 haystack 上的自动完成功能。
http://readthedocs.org/docs/django-haystack/en/v1.2.4/ autocomplete.html
第一部分看起来不错:“设置数据”看起来很简单。但是,我不确定“执行查询”需要写在哪里:即我应该包含在哪个视图中:
from haystack.query import SearchQuerySet
sqs = SearchQuerySet().filter(content_auto=request.GET.get('q', ''))
我当前的 urls.py 很简单,设置如下:
urlpatterns = patterns('',
# Examples:
('^hello/$', hello),
(r'^$', hello),
(r'^search/', include('haystack.urls')),
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
#url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
查看以下博客:
http://tech.agilitynerd.com/haystack-search-result-ordering-and-pre-rende
我愿意具有类似以下内容:
url(r'^search/', SearchView(load_all=False,searchqueryset=sqs),name='haystack_search'),
但是,应该在哪里指定 sqs ?在urls.py 或views.py 中?我尝试了两者,但他们给了我一个名称错误“request”,在 sqs 语句中找不到。
I am trying to use django-haystack (been around 2 days now), and I have got the basic Getting Started example working and it looks very impressive. I would now like to try the autocomplete function on haystack.
http://readthedocs.org/docs/django-haystack/en/v1.2.4/autocomplete.html
The first part seems fine: "Setting up the data" seems simple enough. However, I am not sure where the "Performing the Query" needs to be written: i.e in which view should I include:
from haystack.query import SearchQuerySet
sqs = SearchQuerySet().filter(content_auto=request.GET.get('q', ''))
My current urls.py is simple and set up as follows:
urlpatterns = patterns('',
# Examples:
('^hello/
Looking at the following blog:
http://tech.agilitynerd.com/haystack-search-result-ordering-and-pre-rende
I would like to have something like:
url(r'^search/', SearchView(load_all=False,searchqueryset=sqs),name='haystack_search'),
but, where should the sqs be specified? in the urls.py or views.py? I tried both, but they give me a name error "request" not found on the sqs statement.
, hello),
(r'^
Looking at the following blog:
http://tech.agilitynerd.com/haystack-search-result-ordering-and-pre-rende
I would like to have something like:
but, where should the sqs be specified? in the urls.py or views.py? I tried both, but they give me a name error "request" not found on the sqs statement.
, hello),
(r'^search/', include('haystack.urls')),
# url(r'^
Looking at the following blog:
http://tech.agilitynerd.com/haystack-search-result-ordering-and-pre-rende
I would like to have something like:
but, where should the sqs be specified? in the urls.py or views.py? I tried both, but they give me a name error "request" not found on the sqs statement.
, 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
#url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
Looking at the following blog:
http://tech.agilitynerd.com/haystack-search-result-ordering-and-pre-rende
I would like to have something like:
but, where should the sqs be specified? in the urls.py or views.py? I tried both, but they give me a name error "request" not found on the sqs statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常这会在您自己的 haystack url 中,当前 urls.py 中的 haystack.urls 指向默认 url。创建一个文件 haystack_urls.py 并在 urls.py 中添加它,例如
在该文件中,然后您可以添加自定义代码,例如
包装请求的视图,尝试类似的操作
Usually this would be in your own haystack urls,
haystack.urls
in your current urls.py is pointing to the default urls. Create a file haystack_urls.py and in your urls.py add it e.g.in that file you can then add your custom code e.g.
to wrap a view for request try something like
JamesO 的解决方案对我来说不起作用,所以我定义了一个自定义表单类,它实际上仅覆盖了“搜索”方法。因此,一般来说,您必须尝试
FacetedSearchView
代替basic_search
或SearchView
。创建您自己的表单类,覆盖搜索方法:
在
FacetedSearchView
中指定form_class
form_class=FacetedSearchFormWithAuto
The solution of JamesO was not working for me, so i defined a custom form class, which is actually overrides the 'search' method only. So, in general, you've got to try
FacetedSearchView
insted ofbasic_search
orSearchView
.Create your own form class, overriding the search method:
specify
form_class
inFacetedSearchView
form_class=FacetedSearchFormWithAuto
如果您需要自动完成方面的帮助,我认为这对您有好处,它是完整的解决方案
Django-haystack 全文搜索可以工作,但构面不行
If you need help with autocomplete, I think this would be good for you, it's complete solution
Django-haystack full text search working but facets don't